UserController.php 14 KB

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