UserController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. namespace mall\controllers;
  3. use biz\shop\classes\ShopExtClass;
  4. use bizHd\birthday\classes\BirthdayGiftClass;
  5. use bizHd\custom\classes\CustomClass;
  6. use bizHd\wx\classes\WxOpenClass;
  7. use bizMall\message\services\SmsService;
  8. use bizMall\user\classes\UserClass;
  9. use bizMall\user\classes\UserGrowthClass;
  10. use bizMall\user\classes\UserIntegralClass;
  11. use bizMall\user\services\UserAssetService;
  12. use bizMall\user\services\UserService;
  13. use biz\shop\classes\ShopClass;
  14. use common\components\dict;
  15. use common\components\imgUtil;
  16. use common\components\jwt;
  17. use common\components\noticeUtil;
  18. use common\components\stringUtil;
  19. use Yii;
  20. use common\components\util;
  21. use yii\helpers\Json;
  22. use Overtrue\ChineseCalendar\Calendar;
  23. class UserController extends BaseController
  24. {
  25. //二维码收款使用 ssh
  26. public $guestAccess = ['discount', 'mini-mobile', 'create', 'account-login', 'current-info'];
  27. //清空登录信息 ssh 20250215
  28. public function actionClearLogin()
  29. {
  30. $user = $this->user;
  31. $user->miniOpenId = '';
  32. $user->save();
  33. util::complete();
  34. }
  35. //修改信息 ssh 20221015
  36. public function actionUpdate()
  37. {
  38. $post = Yii::$app->request->post();
  39. $user = $this->user;
  40. if (empty($user)) {
  41. util::fail('请先登录');
  42. }
  43. $userId = $user->id ?? 0;
  44. $address = $post['address'] ?? '';
  45. $floor = $post['floor'] ?? '';
  46. $fullAddress = $address . $floor;
  47. $post['fullAddress'] = $fullAddress;
  48. UserClass::updateUserInfo($user, $post);
  49. util::complete();
  50. }
  51. //修改生日 ssh 20250830
  52. public function actionModifyBirthday()
  53. {
  54. date_default_timezone_set('PRC');
  55. $get = Yii::$app->request->get();
  56. $lunar = $get['lunar'] ?? 0;
  57. $birthdayMonth = $get['birthdayMonth'] ?? 1;
  58. if ($birthdayMonth > 12 || $birthdayMonth < 1) {
  59. util::fail('月份必须1到12月');
  60. }
  61. $birthdayDate = $get['birthdayDate'] ?? 1;
  62. if ($birthdayDate > 31 || $birthdayDate < 1) {
  63. util::fail('日期必须1到31号');
  64. }
  65. if ($lunar == 1 && $birthdayDate == 31) {
  66. util::fail('农历没有31号');
  67. }
  68. $user = $this->user;
  69. if (getenv('YII_ENV') != 'dev' && $user->birthdaySet == 1) {
  70. util::fail('你已经设置过了');
  71. }
  72. if ($lunar == 1) {
  73. try {
  74. //农历转阳历,有多处使用,需要同步修改,关键词 change_my_birthday
  75. $calendar = new Calendar();
  76. $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
  77. $month = $result['gregorian_month'] ?? 1;
  78. $date = $result['gregorian_day'] ?? 1;
  79. $birthday = date("Y") . '-' . $month . '-' . $date;
  80. } catch (\InvalidArgumentException $e) {
  81. $msg = $e->getMessage();
  82. $birthday = '0000-00-00 00:00:00';
  83. util::fail($msg);
  84. }
  85. } else {
  86. $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
  87. }
  88. $birthdayTime = strtotime($birthday);
  89. $user->lunar = $lunar;
  90. $user->birthdayDate = $birthdayDate;
  91. $user->birthdayMonth = $birthdayMonth;
  92. $user->birthday = $birthday;
  93. $user->birthdayTime = $birthdayTime;
  94. $user->birthdaySet = 1;
  95. $user->save();
  96. // 同步变更客户表(xhCustom)中的数据
  97. $customList = \bizMall\custom\classes\CustomClass::getAllByCondition(['userId' => $user->id], null, '*', null, true);
  98. if (!empty($customList)) {
  99. foreach ($customList as $custom) {
  100. $custom->lunar = $lunar;
  101. $custom->birthdayDate = $birthdayDate;
  102. $custom->birthdayMonth = $birthdayMonth;
  103. $custom->birthday = $birthday;
  104. $custom->birthdayTime = $birthdayTime;
  105. $custom->save();
  106. }
  107. }
  108. // 同步变更生日赠礼记录表(xhBirthdayGift)中的数据(只更新当年年份的,往年的不去修改)
  109. $birthdayGiftList = BirthdayGiftClass::getAllByCondition(['userId' => $user->id, 'year' => date("Y")], null, '*', null, true);
  110. if (!empty($birthdayGiftList)) {
  111. foreach ($birthdayGiftList as $birthdayGift) {
  112. list($giftBirthdayMonth, $giftBirthdayDate) = BirthdayGiftClass::convertBirthdayMonthDay(
  113. $lunar,
  114. $birthdayMonth,
  115. $birthdayDate
  116. );
  117. $birthdayGift->lunar = $lunar;
  118. $birthdayGift->birthdayMonth = $giftBirthdayMonth;
  119. $birthdayGift->birthdayDate = $giftBirthdayDate;
  120. $birthdayGift->birthdayDisplay = BirthdayGiftClass::formatBirthdayDisplay($custom, $giftBirthdayMonth, $giftBirthdayDate);
  121. $birthdayGift->save();
  122. }
  123. }
  124. util::complete('修改成功');
  125. }
  126. //获取当前登录用户的信息 ssh 20221014
  127. public function actionCurrentInfo()
  128. {
  129. $user = $this->user;
  130. if (!empty($user)) {
  131. $birthday = $user->birthday ?? '';
  132. //如果当年的生日时间没有更新,则需要给更新
  133. $year = date("Y", strtotime($birthday));
  134. $thisYear = date("Y");
  135. if ($year != $thisYear) {
  136. $lunar = $user->lunar ?? 0;
  137. $userId = $this->userId;
  138. $birthdayMonth = $user->birthdayMonth ?? 0;
  139. $birthdayDate = $user->birthdayDate ?? 0;
  140. if ($lunar == 1 && $birthdayMonth > 0 && $birthdayDate > 0) {
  141. try {
  142. //农历转阳历,有多处使用,需要同步修改,关键词change_my_birthday
  143. $calendar = new Calendar();
  144. $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
  145. $month = $result['gregorian_month'] ?? 1;
  146. $date = $result['gregorian_day'] ?? 1;
  147. $birthday = date("Y") . '-' . $month . '-' . $date;
  148. $user->birthday = $birthday;
  149. $user->save();
  150. CustomClass::updateByCondition(['userId' => $userId], ['birthday' => $birthday]);
  151. } catch (\InvalidArgumentException $e) {
  152. $msg = $e->getMessage();
  153. noticeUtil::push("更新生日出错了呢,userId:" . $userId . ' ' . $msg, '15280215347');
  154. }
  155. } else {
  156. $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
  157. $user->birthday = $birthday;
  158. $user->save();
  159. CustomClass::updateByCondition(['userId' => $userId], ['birthday' => $birthday]);
  160. }
  161. }
  162. // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  163. $user = $user->toArray();
  164. $shortAvatar = $user['avatar'] ?? '';
  165. $smallAvatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  166. $avatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  167. $user['avatar'] = $avatar;
  168. $user['smallAvatar'] = $smallAvatar;
  169. $user['shortAvatar'] = $shortAvatar;
  170. }
  171. // 补充店铺信息
  172. $shop = [];
  173. if (!empty($this->shop)) {
  174. $intraCityRet = ShopClass::hasIntraCity($this->shop);
  175. $shop['openIntraCity'] = $intraCityRet['openIntraCity'];
  176. $shop['hcFreeKm'] = $intraCityRet['hcFreeKm'];
  177. $shop['hcMap'] = $intraCityRet['hcMap'];
  178. }
  179. util::success(['info' => $user, 'shop' => $shop]);
  180. }
  181. //使用账号登录 ssh 20220920
  182. public function actionAccountLogin()
  183. {
  184. $getParams = Yii::$app->request->get();
  185. $postParams = Yii::$app->request->post();
  186. $allParams = array_merge($getParams, $postParams);
  187. $mobile = $allParams['mobile'] ?? '';
  188. $password = $allParams['password'] ?? '';
  189. if ($password != 'a.1234') {
  190. util::fail('密码错误');
  191. }
  192. $user = UserClass::getByCondition(['mobile' => $mobile], true);
  193. if (empty($user)) {
  194. util::fail('账号无效');
  195. }
  196. $userId = $user->id ?? 0;
  197. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  198. // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  199. $user = $user->toArray();
  200. $user['hasMap'] = dict::getDict('hasMap');
  201. util::success(['user' => $user, 'token' => $token]);
  202. }
  203. //生成用户和花店增加客户 ssh 20250307
  204. public function actionCreate()
  205. {
  206. $post = Yii::$app->request->post();
  207. $user = UserClass::replaceUser($post);
  208. if (!empty($this->shop)) {
  209. //花店增加客户
  210. CustomClass::buildRelation($this->shop, $user);
  211. }
  212. $userId = $user->id ?? 0;
  213. $token = !empty($userId) ? jwt::getNewToken($userId) : '';
  214. $fullUser = UserClass::getById($userId, true);
  215. // 将 $fullUser 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
  216. $fullUser = $fullUser->toArray();
  217. $fullUser['hasMap'] = dict::getDict('hasMap');
  218. util::success(['user' => $fullUser, 'token' => $token]);
  219. }
  220. //最近店铺 ssh 2021.2.27
  221. public function actionRecentShop()
  222. {
  223. $list = [];
  224. //显示国兰的信息 0不是 1是
  225. $showGlInfo = getenv('SHOW_GL_INFO');
  226. $info = $showGlInfo === false ? 1 : $showGlInfo;
  227. util::success(['list' => $list, 'showGlInfo' => $info]);
  228. }
  229. //获取登陆客户的资产 ssh 2019.11.22
  230. public function actionAsset()
  231. {
  232. $asset = UserAssetService::getByUserId($this->userId);
  233. util::success($asset);
  234. }
  235. //获取登陆客户的优惠情况 ssh 2019.12.3
  236. public function actionDiscount()
  237. {
  238. $discount = UserAssetService::getDiscount($this->userId);
  239. util::success($discount);
  240. }
  241. //小程序用户完整信息 ssh 2019.12.12
  242. public function actionMiniFullInfo()
  243. {
  244. $post = Yii::$app->request->post();
  245. $iv = $post['iv'] ?? '';
  246. $encryptedData = $post['encryptedData'] ?? '';
  247. $merchant = WxOpenClass::getMallWxMiniBase();
  248. $appId = $merchant['miniAppId'];
  249. $miniOpenId = $post['miniOpenId'];
  250. if (empty($miniOpenId)) {
  251. util::success(['hasNoOpenId' => 1], '登录失败');
  252. }
  253. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  254. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  255. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  256. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  257. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  258. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  259. if ($errCode != 0) {
  260. Yii::info($result . ' ' . $errCode);
  261. util::fail('获取用户信息失败');
  262. }
  263. Yii::info('小程序获取用户信息:' . $result);
  264. $originalInfo = Json::decode($result);
  265. util::success($originalInfo);
  266. }
  267. //小程序用户手机号
  268. public function actionMiniMobile()
  269. {
  270. $post = Yii::$app->request->post();
  271. $iv = $post['iv'];
  272. $encryptedData = $post['encryptedData'];
  273. $merchant = WxOpenClass::getMallWxMiniBase();
  274. $appId = $merchant['miniAppId'];
  275. $miniOpenId = $post['miniOpenId'];
  276. if (empty($miniOpenId)) {
  277. util::success(['hasNoOpenId' => 1], '登录失败');
  278. }
  279. $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
  280. $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  281. if (empty($sessionKey)) {
  282. util::fail('系统错误,session empty');
  283. }
  284. $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
  285. require_once($wxMiniSecret . '/wxBizDataCrypt.php');
  286. $pc = new \WXBizDataCrypt($appId, $sessionKey);
  287. $errCode = $pc->decryptData($encryptedData, $iv, $result);
  288. if ($errCode != 0) {
  289. util::fail('解密失败');
  290. }
  291. $arr = Json::decode($result);
  292. $mobile = $arr['purePhoneNumber'] ?? '';
  293. if (empty($mobile)) {
  294. util::fail('没有取到手机号,请重试');
  295. }
  296. util::success(['mobile' => $mobile]);
  297. }
  298. //密码修改
  299. public function actionPassword()
  300. {
  301. $post = Yii::$app->request->post();
  302. $old = $post['old'] ?? '';
  303. $new = $post['new'] ?? '';
  304. $confirm = $post['confirm'] ?? '';
  305. if (empty($new)) {
  306. util::fail('请输入新密码');
  307. }
  308. if ($new != $confirm) {
  309. util::fail('二次密码不一致');
  310. }
  311. $userId = $this->userId;
  312. $user = UserService::getUserInfo($userId);
  313. if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
  314. if (empty($old)) {
  315. util::fail('请填写旧密码');
  316. }
  317. if (!password_verify($old, $user['payPassword'])) {
  318. util::fail('旧密码错误');
  319. }
  320. }
  321. UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
  322. util::complete('修改成功');
  323. }
  324. //申请会员 ssh 2019.12.18
  325. public function actionApplyMember()
  326. {
  327. $post = Yii::$app->request->post();
  328. $mobile = $post['mobile'] ?? 0;
  329. if (!stringUtil::isMobile($mobile)) {
  330. util::fail('请输入手机号');
  331. }
  332. $userId = $this->userId;
  333. $asset = UserAssetService::getByUserId($userId);
  334. if (isset($asset['member']) && $asset['member'] != -1) {
  335. util::fail('已经是会员了');
  336. }
  337. $code = $post['code'] ?? '';
  338. $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
  339. if (empty($cacheCode) || $cacheCode != $code) {
  340. util::fail('验证码错误');
  341. }
  342. $update = [];
  343. if (!empty($post['realName'])) {
  344. $update['realName'] = $post['realName'];
  345. }
  346. if (!empty($post['birthday'])) {
  347. $update['birthday'] = $post['birthday'];
  348. }
  349. $user = UserService::getByCondition(['sjId' => $this->sjId, 'mobile' => $mobile]);
  350. if ($userId == $user['id']) {
  351. util::fail('手机号已验证');
  352. }
  353. if (!empty($user)) {
  354. util::fail('手机号已经注册过了');
  355. }
  356. $update['mobile'] = $mobile;
  357. $userInfo = $this->user->attributes ?? [];
  358. UserService::becomeMember($userInfo, $this->sj->attributes, $update);
  359. util::complete('注册成功');
  360. }
  361. //登陆客户的信息 ssh 2020.3.12
  362. public function actionLoginDetail()
  363. {
  364. $user = $this->user->attributes ?? [];
  365. util::success($user);
  366. }
  367. //成长值列表
  368. public function actionGrowthList()
  369. {
  370. $where = [];
  371. $where['shopId'] = $this->shopId;
  372. $where['userId'] = $this->userId;
  373. $list = UserGrowthClass::getGrowthList($where);
  374. util::success($list);
  375. }
  376. //积分列表
  377. public function actionIntegralList()
  378. {
  379. $where = [];
  380. $where['shopId'] = $this->shopId;
  381. $where['userId'] = $this->userId;
  382. $list = UserIntegralClass::getIntegralList($where);
  383. util::success($list);
  384. }
  385. }