order(['create_time' => 'desc']) ->where($data) ->paginate(15); } /** * 新增记录 * @param $data * @param array $user * @return bool|false|int * @throws \think\exception\DbException */ public function add($data, $user = []) { if (empty($data['name'])) { $this->error = '请填写商户名称'; return false; } $this->startTrans(); try { $data['orgNo'] = $this->createNo(); $data['appKey'] = $this->suiji(30); $data['store_user_id'] = $user['store_user_id']; $data['store_id'] = self::$storeId; $data = array_merge($data, $this->secretKey()); $this->save($data); //创建商户登录账号 $userModel = new UserModel(); $newData['user_name'] = $data['name']; $newData['password'] = encryption_hash('123456'); $newData['real_name'] = $data['name']; $newData['status'] = 1; $newData['is_super'] = 0; $newData['store_id'] = self::$storeId; //账号层级 $newData['level'] = 1; $newData['partners_id'] = json_encode([$this->partners_id],320); $userModel->save($newData); // 新增角色关系记录 UserRole::increased((int)$userModel['store_user_id'], [10014]); $this->commit(); return true; } catch (\Exception $e) { $this->error = $e->getMessage(); $this->rollback(); return false; } } /*返回随机数*/ public function suiji($digit=12){ $a = "abcdefghijklmnopqrstuvwxyz"; $x = strlen($a)-1; $html = ""; for($i=1;$i<=$digit;$i++) { $rand = rand(1,$x); $html .= substr($a,$rand,1); if($i == 1){//某些语言变量首字母不能是数字 $a .= "0123456789"; $x = strlen($a)-1; } } return $html; } public function secretKey(){ // 生成 RSA 密钥对 $config = [ 'private_key_bits' => 2048, 'private_key_type' => OPENSSL_KEYTYPE_RSA, ]; $keyPair = openssl_pkey_new($config); // 提取私钥(PEM格式) openssl_pkey_export($keyPair, $privateKey); // 提取公钥(PEM格式) $keyDetails = openssl_pkey_get_details($keyPair); $publicKey = $keyDetails['key']; // 直接返回原始 PEM 字符串,不进行 base64 编码 return [ 'privateKey' => $privateKey, 'publicKey' => $publicKey, ]; } /** 生成编号 * @param $prefix * @return string */ public static function createNo($prefix = 'S'): string { return $prefix . substr(implode('', array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8); } /** * 更新记录 * @param array $data * @return bool * @throws \think\exception\DbException */ public function edit($data) { if (empty($data['name'])) { $this->error = '请填写商户名称'; return false; } $this->startTrans(); try { // 更新管理员记录 $this->save($data); $this->commit(); return true; } catch (\Exception $e) { $this->error = $e->getMessage(); $this->rollback(); return false; } } /** * 软删除 * @return false|int */ public function setDelete() { return $this->delete(); } /** * 用户数据统计 * @param $user * @param $get * @return \think\Paginator * @throws \think\exception\DbException */ public function getPartnersStatistics($user,$get = []) { $filter = []; if($user['is_super'] == 0){ $partners_idArr = $user['partners_id']?json_decode($user['partners_id'],true):[]; $filter[] = ['a.partners_id', 'in', $partners_idArr]; } if(isset($get['name'])){ $filter[] = ['a.name', 'like', '%'.$get['name'].'%']; } $times = []; if(isset($get['betweenTime'])) { $times = between_time($get['betweenTime']); } $and = ""; if(isset($times['start_time'])){ $and .= " and create_time >= '".$times['start_time']."' "; } if(isset($times['end_time'])){ $and .= " and create_time < '".($times['end_time'] + 86400)."' "; } $and2 = " and create_time >= '".strtotime(date('Y-m-d'))."' and create_time < '".strtotime(date('Y-m-d').' 23:59:59')."' "; return $this->alias('a') ->field('a.*, (select IFNULL(sum(amount),0) from yoshop_order where partners_id = a.partners_id and order_status = 20 '.$and.') as amount, (select IFNULL(sum(amount),0) from yoshop_order where partners_id = a.partners_id and order_status = 20 '.$and2.') as today') ->order(['a.create_time' => 'desc']) ->where($filter) ->paginate(15); } /** * 商户充值 * @param string $target 充值类型 * @param array $data 表单数据 * @return bool */ public function recharge(string $target, array $data) { // 当前操作人用户名 $storeUserName = StoreUserService::getLoginInfo()['user']['user_name']; if ($target === self::RECHARGE_TYPE_BALANCE) { return $this->rechargeToBalance($storeUserName, $data['balance']); } return false; } /** * 商户充值:余额 * @param string $storeUserName * @param array $data * @return bool */ private function rechargeToBalance(string $storeUserName, array $data) { if (!isset($data['money']) || $data['money'] === '' || $data['money'] < 0) { $this->error = '请输入正确的金额'; return false; } // 判断充值方式,计算最终金额 if ($data['mode'] === 'inc') { $diffMoney = $data['money']; } elseif ($data['mode'] === 'dec') { $diffMoney = -$data['money']; } else { $diffMoney = helper::bcsub($data['money'], $this['balance']); } // 更新记录 $this->transaction(function () use ($storeUserName, $data, $diffMoney) { // 更新账户余额 static::setIncBalance((int)$this['partners_id'], (float)$diffMoney); // 新增余额变动记录 BalanceLogModel::add(SceneEnum::ADMIN, [ 'partners_id' => $this['partners_id'], 'money' => (float)$diffMoney, 'remark' => $data['remark'], ], [$storeUserName]); }); return true; } }