Account.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. namespace app\abutment\logic;
  3. use app\abutment\model\SupplierUser as SupplierUserModel;
  4. use app\abutment\model\SupplierRelationUser as SupplierRelationUserModel;
  5. use app\abutment\model\SupplierUser;
  6. use think\Exception;
  7. use think\facade\Cache;
  8. use think\facade\Config;
  9. use think\facade\Db;
  10. use think\helper\Str;
  11. //供应商账号管理
  12. class Account
  13. {
  14. //登录
  15. public static function login(array $param = [])
  16. {
  17. $db = new SupplierUserModel();
  18. $res = $db->where([
  19. 'is_del' => $db::$is_del_normal,
  20. 'mobile' => $param['mobile']
  21. ])
  22. ->findOrEmpty()
  23. ->toArray();
  24. if (empty($res)) return json_show(1004, '该账号不存在');
  25. if ($res['status'] == $db::$status_disabled) return json_show(1004, '该账号已禁用');
  26. $password = get_encryption_password($param['password'], $res['salt']);
  27. if ($password['password'] != $res['password']) return json_show(1004, '密码错误');
  28. //更新token
  29. $token = Str::random(40, -1);
  30. $expire_int = mt_rand(3600, 7200);
  31. $expire_time = date('Y-m-d H:i:s', time() + $expire_int);
  32. $rs = $db
  33. ->where($db->getPk(), $res[$db->getPk()])
  34. ->save(['token' => $token, 'expire_time' => $expire_time]);
  35. if (!$rs) throw new Exception('更新账号token信息失败');
  36. $list = SupplierRelationUserModel::field('id,supplierNo,supplierName')
  37. ->where([
  38. 'is_del' => $db::$is_del_normal,
  39. 'uid' => $res['uid'],
  40. ])
  41. ->select()
  42. ->toArray();
  43. $info = [
  44. 'uid' => $res['uid'],
  45. 'nickname' => $res['nickname'],
  46. 'mobile' => $res['mobile'],
  47. 'email' => $res['email'],
  48. 'token' => $token,
  49. 'expire_time' => $expire_time,
  50. 'supplier_list' => $list,
  51. ];
  52. //过期时间指定一个具体的日期的时候,还没到这个日期缓存就过期了,指定一个秒数,可以符合预期,邪门儿
  53. Cache::set(Config::get('redis_key.user_info_token') . $token, json_encode($info), $expire_int);
  54. return json_show(0, '登录成功', $info);
  55. }
  56. //获取供应商账号列表
  57. public static function getAccountList(array $param = [])
  58. {
  59. $db = new SupplierUserModel();
  60. $rs = $db
  61. ->where('is_del', $db::$is_del_normal);
  62. if ($param['keyword'] != '') $rs->whereLike('nickname|mobile', '%' . $param['keyword'] . '%');
  63. if ($param['status'] != '') $rs->where('status', $param['status']);
  64. if ($param['supplierNo'] != '') {
  65. $uids = SupplierRelationUserModel::where([
  66. 'is_del' => $db::$is_del_normal,
  67. 'supplierNo' => $param['supplierNo']
  68. ])->column('uid');
  69. $rs->whereIn('uid', $uids);
  70. }
  71. $count = $rs->count('uid');
  72. $list = $rs
  73. ->field('uid,nickname,mobile,email,status,addtime,creater')
  74. ->order('addtime', 'desc')
  75. ->page($param['page'], $param['size'])
  76. ->append(['supplier_list'])
  77. ->withAttr('supplier_list', function ($val, $data) use ($db) {
  78. return SupplierRelationUserModel::where([
  79. 'is_del' => $db::$is_del_normal,
  80. 'uid' => $data['uid']
  81. ])->field('supplierNo,supplierName,status')
  82. ->select()
  83. ->toArray();
  84. })
  85. ->select()
  86. ->toArray();
  87. return json_show(0, '获取成功', ['count' => $count, 'list' => $list]);
  88. }
  89. //修改供应商账号密码
  90. public static function changePassword(array $param = [])
  91. {
  92. $db = new SupplierUserModel();
  93. $res = $db
  94. ->where([
  95. 'uid' => $param['uid'],
  96. 'is_del' => $db::$is_del_normal,
  97. ])
  98. ->field('uid,status')
  99. ->findOrEmpty()
  100. ->toArray();
  101. if (empty($res)) return json_show(1004, '该账号不存在');
  102. if ($res['status'] == $db::$status_disabled) return json_show(1004, '该账号已禁用');
  103. //更新密码
  104. $password = get_encryption_password($param['password']);
  105. $rs = $db
  106. ->where('uid', $res['uid'])
  107. ->save(['password' => $password['password'], 'salt' => $password['salt']]);
  108. return $rs ? json_show(0, '修改密码成功') : json_show(1005, '修改密码失败');
  109. }
  110. //获取供应商账号账号信息
  111. public static function readAccount(array $param = [])
  112. {
  113. $res = SupplierUser::field('uid,nickname,mobile,email')
  114. ->where([
  115. 'uid' => $param['uid'],
  116. 'is_del' => SupplierUser::$is_del_normal,
  117. ])
  118. ->append(['supplier_list'])
  119. ->withAttr('supplier_list', function ($val, $data) {
  120. return SupplierRelationUserModel::field('id,supplierNo,supplierName,status,addtime')
  121. ->where([
  122. 'is_del' => SupplierUser::$is_del_normal,
  123. 'uid' => $data['uid']
  124. ])
  125. ->select()
  126. ->toArray();
  127. })
  128. ->findOrEmpty()
  129. ->toArray();
  130. return json_show(0, '获取账号信息成功', $res);
  131. }
  132. //添加供应商账号
  133. public static function addAccount(array $param = [], string $token = '')
  134. {
  135. $user = GetUserInfo($token);
  136. //查找供应商名称
  137. $supplierName = Db::name('supplier')
  138. ->whereIn('code', $param['supplierNo'])
  139. ->column('name', 'code');
  140. if (empty($supplierName)) return json_show(1004, '供应商不存在');
  141. Db::connect('mysql_sys')->startTrans();
  142. try {
  143. $db = new SupplierUserModel();
  144. $res = $db
  145. ->field('uid')
  146. ->where([
  147. 'is_del' => $db::$is_del_normal,
  148. 'mobile' => $param['mobile'],
  149. ])->findOrEmpty();
  150. if (!$res->isEmpty()) throw new Exception('该手机号已存在');
  151. //新增账号
  152. $password = get_encryption_password(Config::get('app.default_password'));
  153. $uid = $db->insertGetId([
  154. 'nickname' => $param['nickname'],//姓名
  155. 'mobile' => $param['mobile'],//手机号
  156. 'email' => $param['email'],//邮箱
  157. 'password' => $password['password'],//密码密文
  158. 'salt' => $password['salt'],//盐值
  159. 'status' => $db::$status_normal,
  160. 'is_del' => $db::$is_del_normal,
  161. 'createrid' => $user['data']['user_id'] ?? 0,
  162. 'creater' => $user['data']['nickname'] ?? '',
  163. ]);
  164. $insert_data = [];
  165. foreach ($param['supplierNo'] as $supplierNo) {
  166. $insert_data[] = [
  167. 'uid' => $uid,
  168. 'supplierNo' => $supplierNo,
  169. 'supplierName' => $supplierName[$supplierNo] ?? '',
  170. 'status' => $db::$status_normal,
  171. 'is_del' => $db::$is_del_normal,
  172. 'createrid' => $user['data']['user_id'] ?? 0,
  173. 'creater' => $user['data']['nickname'] ?? '',
  174. ];
  175. }
  176. if ($insert_data) Db::connect('mysql_sys')
  177. ->name('supplier_relation_user')
  178. ->insertAll($insert_data);
  179. Db::connect('mysql_sys')->commit();
  180. return json_show(0, '操作成功');
  181. } catch (Exception $exception) {
  182. Db::connect('mysql_sys')->rollback();
  183. return json_show(1005, '操作失败' . $exception->getMessage());
  184. }
  185. }
  186. //修改供应商账号
  187. public static function editAccount(array $param = [])
  188. {
  189. $user = GetUserInfo($param['token']);
  190. Db::connect('mysql_sys')->startTrans();
  191. try {
  192. $db = new SupplierUserModel();
  193. $res = $db
  194. ->field('uid,mobile')
  195. ->where(['uid' => $param['uid'], 'is_del' => $db::$is_del_normal])
  196. ->findOrEmpty()
  197. ->toArray();
  198. if (empty($res)) return json_show(1004, '该账号不存在');
  199. if ($res['mobile'] != $param['mobile']) {
  200. $temp = $db
  201. ->field('uid')
  202. ->where(['mobile' => $param['mobile'], 'is_del' => $db::$is_del_normal])
  203. ->where('uid', '<>', $param['uid'])
  204. ->findOrEmpty()
  205. ->isEmpty();
  206. if (!$temp) throw new Exception('要修改的手机号已存在');
  207. }
  208. $db
  209. ->where('uid', $param['uid'])
  210. ->strict(false)
  211. ->save($param);
  212. $relation_db = new SupplierRelationUserModel();
  213. $insert = $retain = [];
  214. foreach ($param['supplier_list'] as $supplier) {
  215. if (isset($supplier['id']) && $supplier['id'] != 0) $retain[] = $supplier['id'];
  216. else $insert[] = [
  217. 'uid' => $param['uid'],
  218. 'supplierNo' => $supplier['supplierNo'],
  219. 'supplierName' => $supplier['supplierName'],
  220. 'status' => $db::$status_normal,
  221. 'is_del' => $db::$is_del_normal,
  222. 'createrid' => $user['data']['user_id'] ?? 0,
  223. 'creater' => $user['data']['nickname'] ?? 0,
  224. ];
  225. }
  226. //除了保留id,其余删除
  227. if (!empty($retain)) $relation_db->where([
  228. 'is_del' => $db::$is_del_normal,
  229. 'uid' => $param['uid']
  230. ])->whereNotIn('id', $retain)->save(['is_del' => $db::$is_del_deleted]);
  231. if ($insert) $relation_db->saveAll($insert);
  232. Db::connect('mysql_sys')->commit();
  233. return json_show(0, '操作成功');
  234. } catch (Exception $exception) {
  235. Db::connect('mysql_sys')->rollback();
  236. return json_show(1005, '操作失败,' . $exception->getMessage());
  237. }
  238. }
  239. //修改供应商账号状态
  240. public static function statusAccount(array $param = [])
  241. {
  242. Db::connect('mysql_sys')->startTrans();
  243. try {
  244. $db = new SupplierUserModel();
  245. $res = $db
  246. ->field('uid,status')
  247. ->where([
  248. 'uid' => $param['uid'],
  249. 'is_del' => $db::$is_del_normal,
  250. ])->findOrEmpty();
  251. if ($res->isEmpty()) throw new Exception('该账号不存在');
  252. if ($res->status == $param['status']) throw new Exception('不能重复操作');
  253. $db->where([
  254. 'uid' => $param['uid'],
  255. 'is_del' => $db::$is_del_normal,
  256. ])->where('status', '<>', $param['status'])
  257. ->save(['status' => $param['status']]);
  258. SupplierRelationUserModel::where([
  259. 'uid' => $param['uid'],
  260. 'is_del' => $db::$is_del_normal,
  261. ])->where('status', '<>', $param['status'])
  262. ->save(['status' => $param['status']]);
  263. Db::connect('mysql_sys')->commit();
  264. return json_show(0, '操作成功');
  265. } catch (Exception $exception) {
  266. Db::connect('mysql_sys')->rollback();
  267. return json_show(1005, '操作失败,' . $exception->getMessage());
  268. }
  269. }
  270. //删除供应商账号
  271. public static function deleteAccount(int $uid = 0)
  272. {
  273. Db::connect('mysql_sys')->startTrans();
  274. try {
  275. $db = new SupplierUserModel();
  276. $res = $db
  277. ->field('uid')
  278. ->where([
  279. 'uid' => $uid,
  280. 'is_del' => $db::$is_del_normal,
  281. ])->findOrEmpty();
  282. if ($res->isEmpty()) throw new Exception('该账号不存在或已删除');
  283. $db->where('uid', $uid)
  284. ->where('is_del', $db::$is_del_normal)
  285. ->save(['is_del' => $db::$is_del_deleted]);
  286. SupplierRelationUserModel::where('uid', $uid)
  287. ->where('is_del', $db::$is_del_normal)
  288. ->save(['is_del' => $db::$is_del_deleted]);
  289. Db::connect('mysql_sys')->commit();
  290. return json_show(0, '删除成功');
  291. } catch (Exception $exception) {
  292. Db::connect('mysql_sys')->rollback();
  293. return json_show(1005, '删除失败,' . $exception->getMessage());
  294. }
  295. }
  296. }