WechatUser.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/12/21
  6. */
  7. namespace app\models\user;
  8. use app\models\store\StoreCouponIssue;
  9. use crmeb\basic\BaseModel;
  10. use crmeb\services\WechatService;
  11. use crmeb\traits\ModelTrait;
  12. use crmeb\services\SystemConfigService;
  13. use app\models\routine\RoutineQrcode;
  14. use app\models\store\StoreCouponUser;
  15. /**
  16. * TODO 微信用户Model
  17. * Class WechatUser
  18. * @package app\models\user
  19. */
  20. class WechatUser extends BaseModel
  21. {
  22. /**
  23. * 数据表主键
  24. * @var string
  25. */
  26. protected $pk = 'uid';
  27. /**
  28. * 模型名称
  29. * @var string
  30. */
  31. protected $name = 'wechat_user';
  32. use ModelTrait;
  33. /**
  34. * uid获取小程序Openid
  35. * @param string $uid
  36. * @return bool|mixed
  37. */
  38. public static function getOpenId($uid = '')
  39. {
  40. if ($uid == '') return false;
  41. return self::where('uid', $uid)->value('routine_openid');
  42. }
  43. /**
  44. * TODO 用uid获得openid
  45. * @param $uid
  46. * @param string $openidType
  47. * @return mixed
  48. * @throws \Exception
  49. */
  50. public static function uidToOpenid($uid, $openidType = 'routine_openid')
  51. {
  52. $openid = self::where('uid', $uid)->value($openidType);
  53. return $openid;
  54. }
  55. /**
  56. * TODO 用openid获得uid
  57. * @param $openid
  58. * @param string $openidType
  59. * @return mixed
  60. */
  61. public static function openidTouid($openid, $openidType = 'openid')
  62. {
  63. return self::where($openidType, $openid)->where('user_type', '<>', 'h5')->value('uid');
  64. }
  65. /**
  66. * 小程序创建用户后返回uid
  67. * @param $routineInfo
  68. * @return mixed
  69. */
  70. public static function routineOauth($routine)
  71. {
  72. $routineInfo['nickname'] = filter_emoji($routine['nickName']);//姓名
  73. $routineInfo['sex'] = $routine['gender'];//性别
  74. $routineInfo['language'] = $routine['language'];//语言
  75. $routineInfo['city'] = $routine['city'];//城市
  76. $routineInfo['province'] = $routine['province'];//省份
  77. $routineInfo['country'] = $routine['country'];//国家
  78. $routineInfo['headimgurl'] = $routine['avatarUrl'];//头像
  79. $routineInfo['routine_openid'] = $routine['openId'];//openid
  80. $routineInfo['session_key'] = $routine['session_key'];//会话密匙
  81. $routineInfo['unionid'] = $routine['unionId'];//用户在开放平台的唯一标识符
  82. $routineInfo['user_type'] = 'routine';//用户类型
  83. $spid = 0;//绑定关系uid
  84. $isCOde = false;
  85. //获取是否有扫码进小程序
  86. if ($routine['code']) {
  87. if ($info = RoutineQrcode::getRoutineQrcodeFindType($routine['code'])) {
  88. $spid = $info['third_id'];
  89. $isCOde = true;
  90. } else
  91. $spid = $routine['spid'];
  92. } else if ($routine['spid'])
  93. $spid = $routine['spid'];
  94. // 判断unionid 存在根据unionid判断
  95. if ($routineInfo['unionid'] != '' && ($uid = self::where(['unionid' => $routineInfo['unionid']])->where('user_type', '<>', 'h5')->value('uid'))) {
  96. self::edit($routineInfo, $uid, 'uid');
  97. $routineInfo['code'] = $spid;
  98. $routineInfo['isPromoter'] = $isCOde;
  99. if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
  100. User::updateWechatUser($routineInfo, $uid);
  101. } else if ($uid = self::where(['routine_openid' => $routineInfo['routine_openid']])->where('user_type', '<>', 'h5')->value('uid')) { //根据小程序openid判断
  102. self::edit($routineInfo, $uid, 'uid');
  103. $routineInfo['code'] = $spid;
  104. $routineInfo['isPromoter'] = $isCOde;
  105. if ($routine['login_type']) $routineInfo['login_type'] = $routine['login_type'];
  106. User::updateWechatUser($routineInfo, $uid);
  107. } else {
  108. $routineInfo['add_time'] = time();//用户添加时间
  109. $routineInfo = self::create($routineInfo);
  110. $res = User::setRoutineUser($routineInfo, $spid);
  111. $uid = $res->uid;
  112. }
  113. return $uid;
  114. }
  115. /**
  116. * 判断是否是小程序用户
  117. * @param int $uid
  118. * @return bool|int|string
  119. */
  120. public static function isRoutineUser($uid = 0)
  121. {
  122. if (!$uid) return false;
  123. return self::where('uid', $uid)->where('user_type', 'routine')->count();
  124. }
  125. /**
  126. * @param int $uid
  127. * @return int
  128. */
  129. public static function isUserStatus($uid = 0)
  130. {
  131. if (!$uid) return 0;
  132. $user = User::getUserInfo($uid);
  133. return $user['status'];
  134. }
  135. /**
  136. * .添加新用户
  137. * @param $openid
  138. * @return object
  139. */
  140. public static function setNewUser($openid)
  141. {
  142. $userInfo = WechatService::getUserInfo($openid);
  143. if (!isset($userInfo['subscribe']) || !$userInfo['subscribe'] || !isset($userInfo['openid']))
  144. exception('请关注公众号!');
  145. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  146. //判断 unionid 是否存在
  147. if (isset($userInfo['unionid'])) {
  148. $wechatInfo = self::where('unionid', $userInfo['unionid'])->find();
  149. unset($userInfo['qr_scene'],$userInfo['qr_scene_str'],$userInfo['qr_scene_str'],$userInfo['subscribe_scene']);
  150. if ($wechatInfo) {
  151. return self::edit($userInfo->toArray(), $userInfo['unionid'], 'unionid');
  152. }
  153. }
  154. self::beginTrans();
  155. $userInfo['uid'] = User::getkeytoid('uid');
  156. $wechatUser = self::create(is_object($userInfo) ? $userInfo->toArray() : $userInfo);
  157. if (!$wechatUser) {
  158. self::rollbackTrans();
  159. exception('用户储存失败!');
  160. }
  161. if (!User::setWechatUser($wechatUser)) {
  162. self::rollbackTrans();
  163. exception('用户信息储存失败!');
  164. }
  165. self::commitTrans();
  166. self::userFirstSubGiveCoupon($openid);
  167. return $wechatUser;
  168. }
  169. /**
  170. * TODO 关注送优惠券
  171. * @param $openid
  172. */
  173. public static function userFirstSubGiveCoupon($openid)
  174. {
  175. $couponIds = StoreCouponIssue::where('status',1)
  176. ->where('is_give_subscribe',1)
  177. ->where('is_del',0)
  178. ->column('cid');
  179. if ($couponIds)
  180. foreach ($couponIds as $couponId)
  181. if ($couponId)
  182. StoreCouponUser::addUserCoupon(self::openidToUid($openid), $couponId);
  183. }
  184. /**
  185. * 订单金额达到预设金额赠送优惠卷
  186. * @param $uid
  187. */
  188. public static function userTakeOrderGiveCoupon($uid,$total_price)
  189. {
  190. $couponIds = StoreCouponIssue::where('status',1)
  191. ->where('is_full_give',1)
  192. ->where('is_del',0)
  193. ->column('cid,full_reduction');
  194. if ($couponIds)
  195. foreach ($couponIds as $couponId)
  196. if ($couponId)
  197. if ($total_price >= $couponId['full_reduction'])
  198. StoreCouponUser::addUserCoupon($uid, $couponId['cid']);
  199. }
  200. /**
  201. * 更新用户信息
  202. * @param $openid
  203. * @return bool
  204. */
  205. public static function updateUser($openid)
  206. {
  207. $userInfo = WechatService::getUserInfo($openid);
  208. $userInfo['tagid_list'] = implode(',', $userInfo['tagid_list']);
  209. return self::edit($userInfo->toArray(), $openid, 'openid');
  210. }
  211. /**
  212. * 用户存在就更新 不存在就添加
  213. * @param $openid
  214. */
  215. public static function saveUser($openid)
  216. {
  217. self::be($openid, 'openid') == true ? self::updateUser($openid) : self::setNewUser($openid);
  218. }
  219. /**
  220. * 用户取消关注
  221. * @param $openid
  222. * @return bool
  223. */
  224. public static function unSubscribe($openid)
  225. {
  226. return self::edit(['subscribe' => 0], $openid, 'openid');
  227. }
  228. /**
  229. * TODO 用uid获得Unionid
  230. * @param $uid
  231. * @return mixed
  232. */
  233. public static function uidToUnionid($uid)
  234. {
  235. return self::where('uid', $uid)->value('unionid');
  236. }
  237. /**
  238. * TODO 获取用户信息
  239. * @param $openid
  240. * @param $openidType
  241. * @return array
  242. * @throws \think\db\exception\DataNotFoundException
  243. * @throws \think\db\exception\ModelNotFoundException
  244. * @throws \think\exception\DbException
  245. */
  246. public static function getWechatInfo($openid, $openidType)
  247. {
  248. if (is_numeric($openid)) $openid = self::uidToOpenid($openid);
  249. $wechatInfo = self::where($openidType, $openid)->find();
  250. if (!$wechatInfo) {
  251. self::setNewUser($openid);
  252. $wechatInfo = self::where($openidType, $openid)->find();
  253. }
  254. if (!$wechatInfo) exception('获取用户信息失败!');
  255. return $wechatInfo->toArray();
  256. }
  257. }