WechatUser.php 9.7 KB

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