WechatUser.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. //判断 unionid 是否存在
  37. if(isset($userInfo['unionid'])){
  38. $wechatInfo = self::where('unionid',$userInfo['unionid'])->find();
  39. if($wechatInfo){
  40. return self::edit($userInfo,$userInfo['unionid'],'unionid');
  41. }
  42. }
  43. self::beginTrans();
  44. $wechatUser = self::set($userInfo);
  45. if(!$wechatUser){
  46. self::rollbackTrans();
  47. exception('用户储存失败!');
  48. }
  49. if(!User::setWechatUser($wechatUser)){
  50. self::rollbackTrans();
  51. exception('用户信息储存失败!');
  52. }
  53. self::commitTrans();
  54. self::userFirstSubGiveCoupon($openid);
  55. return $wechatUser;
  56. }
  57. /**关注送优惠券
  58. * @param $openid
  59. */
  60. public static function userFirstSubGiveCoupon($openid)
  61. {
  62. $couponId = SystemConfigService::get('wechat_first_sub_give_coupon');
  63. if($couponId) StoreCouponUser::addUserCoupon(self::openidToUid($openid),$couponId);
  64. }
  65. public static function userTakeOrderGiveCoupon($uid)
  66. {
  67. $couponId = SystemConfigService::get('store_order_give_coupon');
  68. if($couponId) StoreCouponUser::addUserCoupon($uid,$couponId);
  69. }
  70. /**
  71. * 更新用户信息
  72. * @param $openid
  73. * @return bool
  74. */
  75. public static function updateUser($openid)
  76. {
  77. $userInfo = WechatService::getUserInfo($openid);
  78. $userInfo['tagid_list'] = implode(',',$userInfo['tagid_list']);
  79. return self::edit($userInfo,$openid,'openid');
  80. }
  81. /**
  82. * 用户存在就更新 不存在就添加
  83. * @param $openid
  84. */
  85. public static function saveUser($openid)
  86. {
  87. self::be($openid,'openid') == true ? self::updateUser($openid) : self::setNewUser($openid);
  88. }
  89. /**
  90. * 用户取消关注
  91. * @param $openid
  92. * @return bool
  93. */
  94. public static function unSubscribe($openid)
  95. {
  96. return self::edit(['subscribe'=>0],$openid,'openid');
  97. }
  98. /**
  99. * 用uid获得openid
  100. * @param $uid
  101. * @return mixed
  102. */
  103. public static function uidToOpenid($uid,$update = false)
  104. {
  105. $cacheName = 'openid_'.$uid;
  106. $openid = Cache::get($cacheName);
  107. if($openid && !$update) return $openid;
  108. $openid = self::where('uid',$uid)->value('openid');
  109. if(!$openid) exception('对应的openid不存在!');
  110. Cache::set($cacheName,$openid,0);
  111. return $openid;
  112. }
  113. /**
  114. * 用uid获得Unionid
  115. * @param $uid
  116. * @return mixed
  117. */
  118. public static function uidToUnionid($uid,$update = false)
  119. {
  120. $cacheName = 'unionid_'.$uid;
  121. $unionid = Cache::get($cacheName);
  122. if($unionid && !$update) return $unionid;
  123. $unionid = self::where('uid',$uid)->value('unionid');
  124. if(!$unionid) exception('对应的unionid不存在!');
  125. Cache::set($cacheName,$unionid,0);
  126. return $unionid;
  127. }
  128. /**
  129. * 用openid获得uid
  130. * @param $uid
  131. * @return mixed
  132. */
  133. public static function openidToUid($openid,$update = false)
  134. {
  135. $cacheName = 'uid_'.$openid;
  136. $uid = Cache::get($cacheName);
  137. if($uid && !$update) return $uid;
  138. $uid = self::where('openid',$openid)->value('uid');
  139. if(!$uid) exception('对应的uid不存在!');
  140. Cache::set($cacheName,$uid,0);
  141. return $uid;
  142. }
  143. /**
  144. * 获取用户信息
  145. * @param $openid
  146. * @return array
  147. */
  148. public static function getWechatInfo($openid)
  149. {
  150. if(is_numeric($openid)) $openid = self::uidToOpenid($openid);
  151. $wechatInfo = self::where('openid',$openid)->find();
  152. if(!$wechatInfo) {
  153. self::setNewUser($openid);
  154. $wechatInfo = self::where('openid',$openid)->find();
  155. }
  156. if(!$wechatInfo) exception('获取用户信息失败!');
  157. return $wechatInfo->toArray();
  158. }
  159. }