UserValidate.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\shopapi\validate;
  20. use app\common\model\User;
  21. use app\common\validate\BaseValidate;
  22. /**
  23. * 用户验证器
  24. * Class UserValidate
  25. * @package app\shopapi\validate
  26. */
  27. class UserValidate extends BaseValidate
  28. {
  29. protected $rule = [
  30. 'pay_password' => 'require|checkPayPassword',
  31. 'transfer_in' => 'require|checkTransferIn',
  32. 'money' => 'require|gt:0|checkMoney',
  33. 'code' => 'require',
  34. 'encrypted_data' => 'require',
  35. 'iv' => 'require',
  36. 'mobile' => 'require|mobile',
  37. 'password' => 'require|length:6,20|alphaDash',
  38. 'old_password' => 'require',
  39. 'inviter_code'=> 'require|checkInviterCode',
  40. ];
  41. protected $message = [
  42. 'pay_password.require' => '请输入支付密码',
  43. 'pay_password.length' => '支付密码须在4-8位之间',
  44. 'pay_password.alphaDash' => '支付密码须为字母数字下划线或破折号',
  45. 'transfer_in.require' => '请选择收款用户',
  46. 'money.require' => '请输入转账金额',
  47. 'money.gt' => '转账金额须大于0',
  48. 'code.require' => '参数缺失',
  49. 'encrypted_data.require' => '参数缺失',
  50. 'iv.require' => '参数缺失',
  51. 'mobile.require' => '请输入手机号码',
  52. 'mobile.mobile' => '无效的手机号码',
  53. 'old_password.require' => '请输入原密码',
  54. 'password.require' => '请输入登录密码',
  55. 'password.length' => '登录密码须在6-20位之间',
  56. 'password.alphaDash' => '登录密码须为字母数字下划线或破折号',
  57. 'inviter_code.require' => '请输入推荐码',
  58. ];
  59. /**
  60. * @notes 余额转账场景
  61. * @return UserValidate
  62. * @author Tab
  63. * @date 2021/8/11 20:27
  64. */
  65. public function sceneTransfer()
  66. {
  67. return $this->only(['pay_password', 'transfer_in', 'money']);
  68. }
  69. /**
  70. * @notes 收款用户信息场景
  71. * @return UserValidate
  72. * @author Tab
  73. * @date 2021/8/12 10:53
  74. */
  75. public function sceneTransferIn()
  76. {
  77. return $this->only(['transfer_in'])
  78. ->remove('transfer_in', 'checkTransferIn');
  79. }
  80. /**
  81. * @notes 设置/修改交易密码场景
  82. * @return UserValidate
  83. * @author Tab
  84. * @date 2021/8/12 11:12
  85. */
  86. public function sceneSetPayPassword()
  87. {
  88. return $this->only(['pay_password'])
  89. ->remove('pay_password', 'checkPayPassword')
  90. ->append('pay_password', 'length:4,8|alphaDash');
  91. }
  92. /**
  93. * @notes 获取微信手机号场景
  94. * @return UserValidate
  95. * @author Tab
  96. * @date 2021/8/24 15:13
  97. */
  98. public function sceneGetMobileByMnp()
  99. {
  100. return $this->only(['code']);
  101. }
  102. /**
  103. * @notes 发送验证码 - 重置支付密码
  104. * @return UserValidate
  105. * @author Tab
  106. * @date 2021/8/24 15:52
  107. */
  108. public function sceneResetPayPasswordCaptcha()
  109. {
  110. return $this->only(['mobile']);
  111. }
  112. /**
  113. * @notes 发送验证码 - 重置登录密码
  114. * @return UserValidate
  115. * @author Tab
  116. * @date 2021/8/25 16:34
  117. */
  118. public function sceneResetPasswordCaptcha()
  119. {
  120. return $this->only(['mobile']);
  121. }
  122. /**
  123. * @notes 发送验证码 - 绑定手机号
  124. * @return UserValidate
  125. * @author Tab
  126. * @date 2021/8/25 16:34
  127. */
  128. public function sceneBindMobileCaptcha()
  129. {
  130. return $this->only(['mobile']);
  131. }
  132. /**
  133. * @notes 发送验证码 - 变更手机号
  134. * @return UserValidate
  135. * @author Tab
  136. * @date 2021/8/25 16:34
  137. */
  138. public function sceneChangeMobileCaptcha()
  139. {
  140. return $this->only(['mobile']);
  141. }
  142. /**
  143. * @notes 绑定手机号场景
  144. * @return UserValidate
  145. * @author Tab
  146. * @date 2021/8/25 17:44
  147. */
  148. public function sceneBindMobile()
  149. {
  150. return $this->only(['mobile', 'code']);
  151. }
  152. /**
  153. * @notes 绑定推荐吗
  154. * @return UserValidate
  155. * @author Tab
  156. * @date 2021/8/25 17:44
  157. */
  158. public function sceneBindInviterCode()
  159. {
  160. return $this->only(['inviter_code']);
  161. }
  162. /**
  163. * @notes 重置支付密码
  164. * @return UserValidate
  165. * @author Tab
  166. * @date 2021/8/24 16:14
  167. */
  168. public function sceneResetPayPassword()
  169. {
  170. return $this->only(['mobile', 'code', 'pay_password'])
  171. ->remove('pay_password', 'checkPayPassword')
  172. ->append('pay_password', 'length:4,8|alphaDash');
  173. }
  174. /**
  175. * @notes 重置登录密码
  176. * @return UserValidate
  177. * @author Tab
  178. * @date 2021/8/25 16:37
  179. */
  180. public function sceneResetPassword()
  181. {
  182. return $this->only(['mobile', 'code', 'password'])
  183. ->append('password', 'require|length:6,12|alphaDash|checkComplexity');
  184. }
  185. /**
  186. * @notes 修改密码
  187. * @return UserValidate
  188. * @author cjhao
  189. * @date 2023/3/7 16:27
  190. */
  191. public function sceneChangePassword()
  192. {
  193. return $this->only(['old_password','password'])
  194. ->append('password','require|length:6,20|alphaDash|checkComplexity');
  195. }
  196. /**
  197. * @notes 设置登录密码
  198. * @return UserValidate
  199. * @author Tab
  200. */
  201. public function sceneSetPassword()
  202. {
  203. return $this->only(['password'])
  204. ->append('password','require|length:6,20|alphaDash|checkComplexity');
  205. }
  206. /**
  207. * @notes 校验推荐码密码
  208. * @param $value
  209. * @param $rule
  210. * @param $data
  211. * @return bool|string
  212. * @author Tab
  213. * @date 2021/8/11 20:35
  214. */
  215. public function checkInviterCode($value, $rule, $data)
  216. {
  217. $user = User::where(['code'=>$data['inviter_code']])->findOrEmpty();
  218. if($user->isEmpty()) {
  219. return '无效的推荐码!';
  220. }
  221. if($user->user_delete) {
  222. return '该推荐码用户已注销!';
  223. }
  224. return true;
  225. }
  226. /**
  227. * @notes 校验支付密码
  228. * @param $value
  229. * @param $rule
  230. * @param $data
  231. * @return bool|string
  232. * @author Tab
  233. * @date 2021/8/11 20:35
  234. */
  235. public function checkPayPassword($value, $rule, $data)
  236. {
  237. $user = User::findOrEmpty($data['user_id']);
  238. if(empty($user->mobile)) {
  239. return '请先绑定手机号';
  240. }
  241. if(empty($user->pay_password)) {
  242. return '请先设置支付密码';
  243. }
  244. if($user->pay_password == md5($value)) {
  245. return true;
  246. }
  247. return '支付密码错误';
  248. }
  249. /**
  250. * @notes 校验收款用户
  251. * @param $value
  252. * @param $rule
  253. * @param $data
  254. * @return bool|string
  255. * @author Tab
  256. * @date 2021/8/11 20:38
  257. */
  258. public function checktransferIn($value, $rule, $data)
  259. {
  260. $me = User::findOrEmpty($data['user_id']);
  261. if($me->sn == $data['transfer_in'] || $me->mobile == $data['transfer_in']) {
  262. return '不能自己转账给自己';
  263. }
  264. $transferIn = User::whereOr('sn', $data['transfer_in'])
  265. ->whereOr('mobile', $data['transfer_in'])
  266. ->findOrEmpty();
  267. if($transferIn->isEmpty()) {
  268. return '收款用户不存在';
  269. }
  270. return true;
  271. }
  272. /**
  273. * @notes 校验余额
  274. * @param $value
  275. * @param $rule
  276. * @param $data
  277. * @return bool|string
  278. * @author Tab
  279. * @date 2021/8/11 20:42
  280. */
  281. public function checkMoney($value, $rule, $data)
  282. {
  283. $user = User::findOrEmpty($data['user_id']);
  284. if($user->user_money < $data['money']) {
  285. return '余额不足';
  286. }
  287. return true;
  288. }
  289. /**
  290. * @notes 校验密码复杂度
  291. * @param $value
  292. * @param $rue
  293. * @param $data
  294. * @author Tab
  295. * @date 2021/12/10 15:06
  296. */
  297. public function checkComplexity($value, $rue, $data)
  298. {
  299. $lowerCase = range('a', 'z');
  300. $upperCase = range('A', 'Z');
  301. $numbers = range(0, 9);
  302. $cases = array_merge($lowerCase, $upperCase);
  303. $caseCount = 0;
  304. $numberCount = 0;
  305. $passwordArr = str_split(trim(($data['password'] . '')));
  306. foreach ($passwordArr as $value) {
  307. if (in_array($value, $numbers)) {
  308. $numberCount++;
  309. }
  310. if (in_array($value, $cases)) {
  311. $caseCount++;
  312. }
  313. }
  314. if ($numberCount >= 1 && $caseCount >= 1) {
  315. return true;
  316. }
  317. return '密码需包含数字和字母';
  318. }
  319. }