WechatUser.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/14
  6. */
  7. namespace app\wap\model\user;
  8. use app\wap\model\store\StoreCoupon;
  9. use app\wap\model\store\StoreCouponUser;
  10. use basic\ModelBasic;
  11. use service\SystemConfigService;
  12. use service\UtilService;
  13. use service\WechatService;
  14. use service\CacheService as Cache;
  15. use think\Session;
  16. use traits\ModelTrait;
  17. class WechatUser extends ModelBasic
  18. {
  19. use ModelTrait;
  20. protected $insert = ['add_time'];
  21. public static function setAddTimeAttr($value)
  22. {
  23. return time();
  24. }
  25. /**
  26. * .添加新用户
  27. * @param $openid
  28. * @return object
  29. */
  30. public static function setNewUser($openid)
  31. {
  32. $userInfo = WechatService::getUserInfo($openid);
  33. if(!isset($userInfo['subscribe']) || !$userInfo['subscribe'] || !isset($userInfo['openid']))
  34. exception('请关注公众号!');
  35. $userInfo['tagid_list'] = implode(',',$userInfo['tagid_list']);
  36. self::beginTrans();
  37. $wechatUser = self::set($userInfo);
  38. if(!$wechatUser){
  39. self::rollbackTrans();
  40. exception('用户储存失败!');
  41. }
  42. if(!User::setWechatUser($wechatUser)){
  43. self::rollbackTrans();
  44. exception('用户信息储存失败!');
  45. }
  46. self::commitTrans();
  47. self::userFirstSubGiveCoupon($openid);
  48. return $wechatUser;
  49. }
  50. public static function userFirstSubGiveCoupon($openid)
  51. {
  52. $couponId = SystemConfigService::get('wechat_first_sub_give_coupon');
  53. if($couponId) StoreCouponUser::addUserCoupon(self::openidToUid($openid),$couponId);
  54. }
  55. public static function userTakeOrderGiveCoupon($uid)
  56. {
  57. $couponId = SystemConfigService::get('store_order_give_coupon');
  58. if($couponId) StoreCouponUser::addUserCoupon($uid,$couponId);
  59. }
  60. /**
  61. * 更新用户信息
  62. * @param $openid
  63. * @return bool
  64. */
  65. public static function updateUser($openid)
  66. {
  67. $userInfo = WechatService::getUserInfo($openid);
  68. $userInfo['tagid_list'] = implode(',',$userInfo['tagid_list']);
  69. return self::edit($userInfo,$openid,'openid');
  70. }
  71. /**
  72. * 用户存在就更新 不存在就添加
  73. * @param $openid
  74. */
  75. public static function saveUser($openid)
  76. {
  77. self::be($openid,'openid') == true ? self::updateUser($openid) : self::setNewUser($openid);
  78. }
  79. /**
  80. * 用户取消关注
  81. * @param $openid
  82. * @return bool
  83. */
  84. public static function unSubscribe($openid)
  85. {
  86. return self::edit(['subscribe'=>0],$openid,'openid');
  87. }
  88. /**
  89. * 用uid获得openid
  90. * @param $uid
  91. * @return mixed
  92. */
  93. public static function uidToOpenid($uid,$update = false)
  94. {
  95. $cacheName = 'openid_'.$uid;
  96. $openid = Cache::get($cacheName);
  97. if($openid && !$update) return $openid;
  98. $openid = self::where('uid',$uid)->value('openid');
  99. if(!$openid) exception('对应的openid不存在!');
  100. Cache::set($cacheName,$openid,0);
  101. return $openid;
  102. }
  103. /**
  104. * 用uid获得Unionid
  105. * @param $uid
  106. * @return mixed
  107. */
  108. public static function uidToUnionid($uid,$update = false)
  109. {
  110. $cacheName = 'unionid_'.$uid;
  111. $unionid = Cache::get($cacheName);
  112. if($unionid && !$update) return $unionid;
  113. $unionid = self::where('uid',$uid)->value('unionid');
  114. if(!$unionid) exception('对应的unionid不存在!');
  115. Cache::set($cacheName,$unionid,0);
  116. return $unionid;
  117. }
  118. /**
  119. * 用openid获得uid
  120. * @param $uid
  121. * @return mixed
  122. */
  123. public static function openidToUid($openid,$update = false)
  124. {
  125. $cacheName = 'uid_'.$openid;
  126. $uid = Cache::get($cacheName);
  127. if($uid && !$update) return $uid;
  128. $uid = self::where('openid',$openid)->value('uid');
  129. if(!$uid) exception('对应的uid不存在!');
  130. Cache::set($cacheName,$uid,0);
  131. return $uid;
  132. }
  133. /**
  134. * 获取用户信息
  135. * @param $openid
  136. * @return array
  137. */
  138. public static function getWechatInfo($openid)
  139. {
  140. if(is_numeric($openid)) $openid = self::uidToOpenid($openid);
  141. $wechatInfo = self::where('openid',$openid)->find();
  142. if(!$wechatInfo) {
  143. self::setNewUser($openid);
  144. $wechatInfo = self::where('openid',$openid)->find();
  145. }
  146. if(!$wechatInfo) exception('获取用户信息失败!');
  147. return $wechatInfo->toArray();
  148. }
  149. }