store_id)) { $this->store_id = get_store_id(); } } public function mchSetting() { $setting = json_decode(Option::get(OptionSetting::MCH_SETTING, $this->store_id, 'store', '{}')['value'], true); $ret = [ 'mch_reg_form' => $setting['mch_reg_form'] ?? [], 'mch_reg_text' => $setting['mch_reg_text'] ?? '', 'mch_goods_audit' => $setting['mch_goods_audit'] ?? 0, 'mch_order_notice' => $setting['mch_order_notice'] ?? 0, 'mch_cash_type' => $setting['mch_cash_type'] ?? [0], 'mch_cash_rate' => $setting['mch_cash_rate'] ?? 0, 'show_mch_mobile' => !isset($setting['show_mch_mobile']) ? 1 : $setting['show_mch_mobile'], 'show_mch_address' => !isset($setting['show_mch_address']) ? 1 : $setting['show_mch_address'], ]; if(!is_array($ret['mch_cash_type'])){ $ret['mch_cash_type'] = [$ret['mch_cash_type']]; } return $ret; } public function mchSettingSave($conf = []) { Option::set(OptionSetting::MCH_SETTING, json_encode($conf), $this->store_id, 'store'); } public function mchList() { try { $is_delete = 0; if ($this->is_delete == 1) { $is_delete = 1; } $query = Mch::find()->with(['mchBrands'])->where(['is_delete' => $is_delete, 'store_id' => $this->store_id]); if (!is_null($this->is_open) && $this->is_open > -1) { $query->andWhere(['is_open' => $this->is_open]); } if (!is_null($this->mch_common_cat_id) && $this->mch_common_cat_id > 0) { $query->andWhere(['mch_common_cat_id' => $this->mch_common_cat_id]); } if ($this->mch_id > 0) { $query->andWhere(['id' => $this->mch_id]); } if (!empty($this->name)) { $query->andWhere(['like', 'name', trim($this->name)]); } if (!empty($this->realname)) { $query->andWhere(['like', 'realname', trim($this->realname)]); } if (!empty($this->tel)) { $query->andWhere(['like', 'tel', trim($this->tel)]); } if (!empty($this->begin_time)) { $query->andWhere(['>=', 'created_at', strtotime($this->begin_time)]); } if (!empty($this->end_time)) { $query->andWhere(['<=', 'created_at', strtotime($this->end_time)]); } if($this->brands_id > -1){ $query->andWhere(['brands_id' => $this->brands_id]); } if($this->district_id > 0){ $query->andWhere(['district_id' => $this->district_id]); } $query->select('*'); $query->orderBy('id DESC'); if($this->orderByAi){ $query->orderBy('account_money_total DESC, sort DESC, id ASC'); } if($this->lng && $this->lat){ $queryDis = 'ROUND(st_distance_sphere(point('. $this->lng .', '. $this->lat .'), point(longitude, latitude)), 2)'; if($this->dis){ $query->andWhere($queryDis . " < :dis", [':dis' => $this->dis]); } $query->addSelect([$queryDis . ' dis']); // $query->andWhere(['AND', '`longitude` > 0 AND `latitude` > 0']); if($this->orderByDis){ $query->orderBy('dis ASC, sort DESC'); } } $pagination = pagination_make($query); $cats = MchCommonCat::find()->where(['store_id' => $this->store_id])->indexBy('id')->all(); foreach ($pagination['list'] as &$item) { $item['mch_common_cat_name'] = $cats[$item['mch_common_cat_id']] ? $cats[$item['mch_common_cat_id']]['name'] : '-'; $saasUser = SaasUser::findOne([$item['saas_user_id']]); $item['saas_user'] = $saasUser; $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']); $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']); $admin = Admin::findOne(['type_id' => $item['id'], 'type' => Admin::ADMIN_TYPE_MCH, 'is_delete' => 0]); if($admin->store_id != $item['store_id']){ $admin->store_id = $item['store_id']; $admin->save(); } $item['admin'] = $admin; if(isset($item['dis'])){ $item['dis_str'] = $item['dis'] > 1000 ? (round($item['dis'] / 1000, 2) . 'km') : (round($item['dis'], 0) . 'm'); } $goods_count = Goods::find()->where(['mch_id' => $item['id'], 'is_delete' => 0])->count(); $item['goods_count'] = $goods_count ?: 0; } return [ 'code' => 0, 'msg' => 'success', 'data' => $pagination, // 'q' => $query->createCommand()->getRawSql(), ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function diyMchSelectList(){ $query = Mch::find()->where(['store_id' => $this->store_id,'is_delete' => 0]) ->select('id,name,logo,mch_common_cat_id'); if($this->name){ $query->andWhere(['like','name', $this->name]); } if($this->mch_common_cat_id){ $query->andWhere(['mch_common_cat_id' => $this->mch_common_cat_id]); } $result = pagination_make($query); foreach($result['list'] as &$item){ // 搜索入住商商品6个 $item['goods_list'] = Goods::find()->where(['mch_id' => $item['id'],'is_delete'=>0,'status' => 1])->select('id,name,cover_pic,original_price,price')->limit(6)->asArray()->all(); } return [ 'code' => 0, 'msg' => 'success', 'data' => $result, ]; } public function mchSelectList() { $list = Mch::find()->where(['store_id' => $this->store_id]) ->select('id,name,logo,mch_common_cat_id,is_delete')->indexBy('id')->asArray()->all(); foreach($list as &$item){ $item['name'] = $item['id'] . '.' . $item['name'] . ($item['is_delete'] ? '(已删)' : ''); } return [ 'code' => 0, 'msg' => 'success', 'data' => $list, ]; } public function mchInfo() { $mch = Mch::findOne($this->mch_id); return [ 'code' => 0, 'msg' => 'success', 'data' => $mch, 'dis' => \app\utils\Tools::getDistance($mch->latitude, $mch->longitude, $this->lat, $this->lng), 'homeTypes' => $this->mchHomeList(), ]; } public function mchHomeList() { $urlPre = ''; if (\Yii::$app instanceof \yii\web\Application){ $urlPre = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl; } $homeTypes = [ $urlPre . '/web/v1/statics/images/mch-home/home1.png', $urlPre . '/web/v1/statics/images/mch-home/home2.png', $urlPre . '/web/v1/statics/images/mch-home/home3.png', ]; return $homeTypes; } public function mchHomeSave() { $mch = Mch::findOne(['id' => $this->id]); $mch->home_type = (int)$this->home_type; if (!$mch->save()) { \Yii::error([__METHOD__, $mch->attributes]); throw new \Exception('保存失败。' . array_shift($mch->getFirstErrors())); } return [ 'code' => 0, 'msg' => '操作成功!', ]; } public function mchSave() { $t = \Yii::$app->db->beginTransaction(); try { $mch = $this->id ? Mch::findOne(['id' => $this->id, 'store_id' => $this->store_id]) : new Mch(); if (empty($mch)) { throw new \Exception('参数错误' . $this->id); } if (isset($this->store_id)) { $mch->store_id = $this->store_id; } if (isset($this->user_id)) { $user = User::findOne($this->user_id); if (empty($user)) { throw new \Exception('用户id错误。' . $this->user_id); } $saasUser = SaasUser::findOne(['mobile' => $user->binding]); $mch->user_id = $this->user_id; $mch->saas_user_id = $saasUser->id; } if (isset($this->realname)) { $mch->realname = $this->realname; } if (isset($this->tel)) { $mch->tel = $this->tel; } if (isset($this->mch_common_cat_id)) { $mch->mch_common_cat_id = $this->mch_common_cat_id; } if (isset($this->wechat_name)) { $mch->wechat_name = $this->wechat_name; } if (isset($this->name)) { $mch->name = $this->name; } if (isset($this->is_open)) { $mch->is_open = $this->is_open; } if (isset($this->logo)) { $mch->logo = $this->logo; } if (isset($this->transfer_rate)) { $mch->transfer_rate = $this->transfer_rate; } if (isset($this->sort)) { $mch->sort = $this->sort; } if (isset($this->brands_id) && $this->brands_id > 0) { $mch->brands_id = $this->brands_id; } $mch->service_tel = ''; if (isset($this->province_id) && $this->province_id) { $district = District::find()->where(['id' => [$this->province_id, $this->city_id, $this->district_id]])->select('name')->column(); $district = implode('', $district); $mch->district = $district; $mch->province_id = $this->province_id; $mch->city_id = $this->city_id; $mch->district_id = $this->district_id; $mch->address = $this->address; $mch->longitude = $this->longitude; $mch->latitude = $this->latitude; } else { $mch->province_id = 0; $mch->city_id = 0; $mch->district_id = 0; $mch->address = ''; } if (!$mch->save()) { \Yii::error([__METHOD__, $mch->attributes]); throw new \Exception('保存失败。' . array_shift($mch->getFirstErrors())); } $this->mchAdminSave($this->id ? 0 : 1, $mch, trim($this->username), trim($this->password), $saasUser->id); $t->commit(); return [ 'code' => 0, 'msg' => '操作成功!', 'data' => $mch, ]; } catch (\Exception $e) { \Yii::error($e); $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function mchAdminSave($isAdd, $mch, $username, $password, $saas_user_id) { $mch_id = $mch->id; if ($isAdd) { if (empty($username)) { throw new \Exception('请输入登陆账户'); } $is = Admin::find()->where(['username' => $username, 'is_delete' => 0])->one(); if ($is) { throw new \Exception('账户名称已经存在!请更换' . $username); } $admin = new Admin(); $admin->access_token = \Yii::$app->security->generateRandomString(); $admin->username = $username; $admin->name = $username; $admin->store_id = $mch->store_id; $admin->type_id = $mch_id; $admin->type = Admin::ADMIN_TYPE_MCH; } else { if (empty($password)) { return; } $admin = Admin::findOne(['type_id' => $mch_id, 'type' => Admin::ADMIN_TYPE_MCH, 'username' => $username, 'is_delete' => 0]); if (empty($admin)) { throw new \Exception('账户不存在'); } } $admin->password = \Yii::$app->security->generatePasswordHash($password ?: '123456'); $admin->saas_user_id = $saas_user_id; if (!$admin->save()) { throw new \Exception('账户保存失败!' . $username . array_shift($admin->getFirstErrors())); } } public function mchOpen() { try { $is_open = $this->is_open; $id = $this->id; if (!is_array($id)) { $id = explode(',', $id); } foreach ($id as $item) { $mch = Mch::findOne($item); if (!$mch) { throw new \Exception('参数错误' . $item); } $mch->is_open = $is_open; if (!$mch->save()) { throw new \Exception('保存失败。' . $item . array_shift($mch->getFirstErrors())); } } return [ 'code' => 0, 'msg' => 'success', ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function mchDel() { try { $id = $this->id; if (!is_array($id)) { $id = explode(',', $id); } foreach ($id as $item) { $mch = Mch::findOne($item); if (!$mch) { throw new \Exception('参数错误' . $item); } $mch->is_delete = 1; if (!$mch->save()) { throw new \Exception('保存失败。' . $item . array_shift($mch->getFirstErrors())); } } return [ 'code' => 0, 'msg' => 'success', ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function mchAuditList() { try { $is_delete = 0; if ($this->is_delete == 1) { $is_delete = 1; } $query = MchAudit::find()->where(['is_delete' => $is_delete, 'store_id' => $this->store_id]); if (!is_null($this->status) && $this->status > -1) { $query->andWhere(['status' => $this->status]); } if (!is_null($this->mch_common_cat_id) && $this->mch_common_cat_id > 0) { $query->andWhere(['mch_common_cat_id' => $this->mch_common_cat_id]); } if ($this->mch_id > 0) { $query->andWhere(['mch_id' => $this->mch_id]); } if ($this->user_id > 0) { $query->andWhere(['user_id' => $this->user_id]); } if (!empty($this->saas_user_name)) { $query->andWhere(['saas_user_id' => SaasUser::find()->select('id')->where(['like', 'name', trim($this->saas_user_name)])]); } if (!empty($this->name)) { $query->andWhere(['like', 'name', trim($this->name)]); } if (!empty($this->realname)) { $query->andWhere(['like', 'realname', trim($this->realname)]); } if (!empty($this->tel)) { $query->andWhere(['like', 'tel', trim($this->tel)]); } if (!empty($this->begin_time)) { $query->andWhere(['>=', 'created_at', strtotime($this->begin_time)]); } if (!empty($this->end_time)) { $query->andWhere(['<=', 'created_at', strtotime($this->end_time)]); } $query->orderBy('id DESC'); $pagination = pagination_make($query); $cats = MchCommonCat::find()->where(['store_id' => $this->store_id])->indexBy('id')->all(); foreach ($pagination['list'] as &$item) { $item['mch_common_cat_name'] = $cats[$item['mch_common_cat_id']] ? $cats[$item['mch_common_cat_id']]['name'] : '-'; $saasUser = SaasUser::findOne([$item['saas_user_id']]); $item['saas_user'] = $saasUser; $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']); $item['updated_at'] = date("Y-m-d H:i:s", $item['updated_at']); } return [ 'code' => 0, 'msg' => 'success', 'data' => $pagination, 'q' => $query->createCommand()->getRawSql(), ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function mchAuditStatus() { $t = \Yii::$app->db->beginTransaction(); try { $reason = $this->reason; $status = $this->status; $id = $this->id; if (!is_array($id)) { $id = explode(',', $id); } foreach ($id as $item) { $mchAudit = MchAudit::findOne($item); if (!$mchAudit) { throw new \Exception('参数错误' . $item); } $mchAudit->reason = $reason ?: ''; $mchAudit->status = $status; if (!$mchAudit->save()) { throw new \Exception('保存失败。' . $item . array_shift($mchAudit->getFirstErrors())); } if ($status == 1) { $mchSave = (new MchForm([ 'username' => $mchAudit['username'], 'password' => $mchAudit['password'], 'user_id' => $mchAudit['user_id'], 'name' => $mchAudit['name'], 'logo' => $mchAudit['logo'], 'realname' => $mchAudit['realname'], 'tel' => $mchAudit['tel'], 'mch_common_cat_id' => $mchAudit['mch_common_cat_id'], 'wechat_name' => $mchAudit['wechat_name'], 'form' => $mchAudit['form'], 'province_id' => $mchAudit['province_id'], 'city_id' => $mchAudit['city_id'], 'district_id' => $mchAudit['district_id'], 'address' => $mchAudit['address'], 'longitude' => $mchAudit['longitude'], 'latitude' => $mchAudit['latitude'], ]))->mchSave(); if ($mchSave['code']) { throw new \Exception('商户创建失败。' . $mchSave['msg']); } $mchAudit->mch_id = $mchSave['data']['id']; if (!$mchAudit->save()) { throw new \Exception('审核关联失败。' . $item . array_shift($mchAudit->getFirstErrors())); } } } $t->commit(); return [ 'code' => 0, 'msg' => 'success', ]; } catch (\Exception $e) { \Yii::error($e); $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function mchReg() { $t = \Yii::$app->db->beginTransaction(); try { $has = MchAudit::find()->where(['user_id' => $this->user_id, 'status' => 0])->one(); if($has){ throw new \Exception('禁止重复申请。'); } $mch = new MchAudit(); $mch->store_id = $this->store_id; $mch->user_id = $this->user_id; $mch->saas_user_id = $this->saas_id; $mch->realname = $this->realname; $mch->tel = $this->tel; $mch->mch_common_cat_id = $this->mch_common_cat_id; $mch->wechat_name = $this->wechat_name; $mch->username = $this->username; $mch->password = $this->password; $mch->form = is_array($this->form) ? json_encode($this->form) : $this->form; if (isset($this->name)) { $mch->name = $this->name; } if (isset($this->logo)) { $mch->logo = $this->logo; } if (isset($this->province_id) && $this->province_id) { $district = District::find()->where(['id' => [$this->province_id, $this->city_id, $this->district_id]])->select('name')->column(); $district = implode('', $district); $mch->district = $district; $mch->province_id = $this->province_id; $mch->city_id = $this->city_id; $mch->district_id = $this->district_id; $mch->address = $this->address; $mch->longitude = $this->longitude; $mch->latitude = $this->latitude; } if (!$mch->save()) { \Yii::error([__METHOD__, $mch->attributes]); throw new \Exception('保存失败。' . array_shift($mch->getFirstErrors())); } $t->commit(); return [ 'code' => 0, 'msg' => '操作成功!', 'data' => $mch, ]; } catch (\Exception $e) { \Yii::error($e); $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function mchAuditDel() { try { $id = $this->id; if (!is_array($id)) { $id = explode(',', $id); } foreach ($id as $item) { $mchAudit = MchAudit::findOne($item); if (!$mchAudit) { throw new \Exception('参数错误' . $item); } $mchAudit->is_delete = 1; if (!$mchAudit->save()) { throw new \Exception('保存失败。' . $item . array_shift($mchAudit->getFirstErrors())); } } return [ 'code' => 0, 'msg' => 'success', ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function mchCommonCatList() { try { $is_delete = 0; if ($this->is_delete == 1) { $is_delete = 1; } $query = MchCommonCat::find()->where(['is_delete' => $is_delete, 'store_id' => $this->store_id]); if (!is_null($this->is_show) && $this->is_show > -1) { $query->andWhere(['is_show' => $this->is_show]); } if (!empty($this->name)) { $query->andWhere(['like', 'name', trim($this->name)]); } if (!empty($this->begin_time)) { $query->andWhere(['>=', 'created_at', strtotime($this->begin_time)]); } if (!empty($this->end_time)) { $query->andWhere(['<=', 'created_at', strtotime($this->end_time)]); } $query->orderBy('sort DESC, id DESC'); $list = $query->asArray()->all(); return [ 'code' => 0, 'msg' => 'success', 'data' => ['list' => $list], // 'q' => $query->createCommand()->getRawSql(), ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function mchCommonCatSelectList($indexBy = 1) { $query = MchCommonCat::find()->where(['is_delete' => 0, 'store_id' => $this->store_id]) ->select('id,name,is_show'); if($indexBy){ $query->indexBy('id'); } $list = $query->orderBy('sort DESC, id DESC')->asArray()->all(); foreach($list as &$item){ if(!$item['is_show']){ $item['name'] .= '(已隐藏)'; } } return [ 'code' => 0, 'msg' => 'success', 'data' => $list, ]; } public function mchCommonCatSave() { try { $mchCommonCat = $this->id ? MchCommonCat::findOne(['id' => $this->id, 'store_id' => $this->store_id]) : new MchCommonCat(); if (empty($mchCommonCat)) { throw new \Exception('参数错误' . $this->id); } $mchCommonCat->name = $this->name; $mchCommonCat->sort = $this->sort ?: 0; $mchCommonCat->is_show = $this->is_show; $mchCommonCat->store_id = $this->store_id; if (!$mchCommonCat->save()) { \Yii::error([__METHOD__, $mchCommonCat->attributes]); throw new \Exception('保存失败。' . array_shift($mchCommonCat->getFirstErrors())); } return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function mchCommonCatShow() { try { $is_show = $this->is_show; $id = $this->id; if (!is_array($id)) { $id = explode(',', $id); } foreach ($id as $item) { $mchCommonCat = MchCommonCat::findOne($item); if (!$mchCommonCat) { throw new \Exception('参数错误' . $item); } $mchCommonCat->is_show = $is_show; if (!$mchCommonCat->save()) { throw new \Exception('保存失败。' . $item . array_shift($mchCommonCat->getFirstErrors())); } } return [ 'code' => 0, 'msg' => 'success', ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function mchCommonCatDel() { try { $id = $this->id; if (!is_array($id)) { $id = explode(',', $id); } foreach ($id as $item) { $mchCommonCat = MchCommonCat::findOne($item); if (!$mchCommonCat) { throw new \Exception('参数错误' . $item); } $mchCommonCat->is_delete = 1; if (!$mchCommonCat->save()) { throw new \Exception('保存失败。' . $item . array_shift($mchCommonCat->getFirstErrors())); } } return [ 'code' => 0, 'msg' => 'success', ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function mchGoodsAudit() { $t = \Yii::$app->db->beginTransaction(); try { $status = $this->status; $id = $this->id; if (!is_array($id)) { $id = explode(',', $id); } foreach ($id as $item) { $goods = Goods::findOne($item); if (!$goods) { throw new \Exception('参数错误' . $item); } $goods->mch_audit = 0; $goods->mch_audit_time = 0; if ($status == 1) { $goods->status = Goods::STATUS_NORMAL; } if (!$goods->save()) { throw new \Exception('保存失败。' . $item . array_shift($goods->getFirstErrors())); } } $t->commit(); return [ 'code' => 0, 'msg' => 'success', ]; } catch (\Exception $e) { \Yii::error($e); $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public static function goodsCatList($mch_id = 1, $is_show = -1){ $res = []; $query = MchCat::find()->where([ 'is_delete' => 0, 'mch_id' => $mch_id ])->orderBy(['sort'=>SORT_ASC]); if ($is_show > -1) { $query->andWhere([ 'is_show' => $is_show ]); } $cat_list = $query->asArray()->all(); if ($cat_list) { $res = buildTree($cat_list); } return $res; } public function goodsCatSave(){ $cat = $this->id ? MchCat::findOne(['id' => $this->id, 'mch_id' => $this->mch_id]) : new MchCat(); if (empty($cat)) { throw new \Exception('参数错误' . $this->id); } $cat->parent_id = $this->parent_id; $cat->is_show = $this->is_show; if ($this->is_show == 0 && $this->id) { // 产品分类隐藏同时下架该分类下所有商品 $goods_ids = MchGoodsCat::find()->where(['cat_id' => $this->id])->select('goods_id')->asArray()->all(); Goods::updateAll(['status' => Goods::STATUS_DISABLE],['in', 'id', array_column($goods_ids, 'goods_id')]); } $cat->name = $this->name; $cat->pic_url = $this->pic_url; $cat->sort = $this->sort; $cat->mch_id = $this->mch_id; if ($cat->save()) { return [ 'code' => 0, 'msg' => '提交成功' ]; } else { return [ 'code' => 0, 'msg' => '保存失败' . array_shift($cat->getFirstErrors()), ]; } } public function goodsCatStatus($id, $status = 1){ try { if (!is_array($id)) { $id = explode(',', $id); } foreach ($id as $item) { $mchCat = MchCat::findOne($item); if (!$mchCat) { throw new \Exception('参数错误' . $item); } if($status == 0 || $status == 1){ $mchCat->is_show = $status; }else{ $mchCat->is_delete = 1; } if (!$mchCat->save()) { throw new \Exception('保存失败。' . $item . array_shift($mchCat->getFirstErrors())); } } return [ 'code' => 0, 'msg' => 'success', ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //发放佣金 public static function accountMoneyAdd($order) { if(!$order){ return [ 'code' => 1, 'msg' => '入驻商订单不存在或已发放', ]; } $mch_id = $order->mch_id; if(!$mch_id){ return [ 'code' => 404, 'msg' => '非入驻商订单', ]; } $order_id = $order->id; $t = \Yii::$app->db->beginTransaction(); try{ $order->mch_is_price = 1; $order->mch_is_price_time = time(); $save = $order->save(); if(!$save){ throw new \Exception('入驻商佣金发放状态修改失败' . array_shift($order->getFirstErrors())); } $mch = Mch::findOne($mch_id); $pay_price = $order instanceof \app\plugins\scanCodePay\models\Order ? $order->total_price : $order->pay_price; $profit = sprintf("%.2f", $pay_price * $mch->transfer_rate / 100); $amount = $pay_price - $profit; $desc = []; // $share_price = (float)$order->first_price + (float)$order->second_price + (float)$order->third_price; if($share_price > 0){ $amount -= $share_price; $desc[] = '分销佣金:' . $share_price; } //同城配送费用 if(isset($order->is_delivery) && $order->is_delivery){ $delivery_info = \app\models\DeliveryInfo::findOne(['order_no' => $order->order_no]); $express_price = (float)$delivery_info['fee']; if($express_price > 0){ $amount -= $express_price; $desc[] = '同城配送费用:' . $express_price; } } $desc = implode(';', $desc); $amount = $amount <= 0 ? 0 : $amount; $saveLog = MchAccountLog::saveLog($mch_id, $order->user_id, $amount, AccountLog::LOG_TYPE_INCOME, $order_id, $order->order_no, $desc, $profit); if($saveLog['code']){ throw new \Exception($saveLog['msg']); } $t->commit(); return [ 'code' => 0, 'msg' => 'ok', ]; } catch (\Exception $ex) { debug_log([__METHOD__, __LINE__, $order->id, $ex->getMessage()], __CLASS__ . '.log'); $t->rollBack(); return [ 'code' => 1, 'msg' => '操作失败,' . $ex->getMessage(), ]; } } //佣金 public function accountLog() { try { $query = MchAccountLog::find()->where(['mch_id' => $this->mch_id]); if ($this->order_no) { $query->andWhere(['like', 'order_no', $this->order_no]); } if (!empty($this->begin_time)) { $query->andWhere(['>=', 'created_at', strtotime($this->begin_time)]); } if (!empty($this->end_time)) { $query->andWhere(['<=', 'created_at', strtotime($this->end_time)]); } $query->orderBy('id DESC'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']); } return [ 'code' => 0, 'msg' => 'success', 'data' => $pagination, 'mch' => Mch::findOne($this->mch_id), // 'q' => $query->createCommand()->getRawSql(), ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } //提现 public function mchCashSubmit($price, $type, $user_id = 0, $name = '', $account = '', $bank = '') { $mch_id = $this->mch_id; $exit = Cash::find()->andWhere(['=', 'status', 0])->andWhere(['mch_id' => $mch_id, 'cash_type' => Cash::IS_CASH_TYPE_MCH])->exists(); if ($exit) { // return [ // 'code' => 1, // 'msg' => '尚有未完成的提现申请' // ]; } $mch = Mch::findOne($mch_id); $conf = $this->mchSetting(); if (!$price) { return [ 'code' => 1, 'msg' => '参数非法1', 'f' => func_get_args(), 'c' => $conf, ]; } $service_charge = $price * $conf['mch_cash_rate'] / 100; $t = \Yii::$app->db->beginTransaction(); try { $cash = new Cash(); $cash->order_no = OrderNo::getOrderNo(OrderNo::ORDER_CASH); $cash->is_delete = 0; $cash->status = 0; $cash->mch_id = $mch_id; $cash->price = $price; $cash->created_at = time(); $cash->user_id = $user_id; $cash->store_id = $mch->store_id; $cash->type = $type; $cash->name = $name; $cash->mobile = $account; $cash->bank_name = $bank; $cash->pay_time = 0; $cash->service_charge = $conf['mch_cash_rate']; $cash->cash_type = Cash::IS_CASH_TYPE_MCH; if (!$cash->save()) { \Yii::error([__METHOD__, $cash->getErrors()]); throw new \Exception('入驻商提现保存失败' . array_shift($cash->getFirstErrors())); } $saveLog = MchAccountLog::saveLog($mch_id, (int)$user_id, $price, AccountLog::LOG_TYPE_EXPEND, 0, $cash->order_no, '提现', $service_charge); if($saveLog['code']){ throw new \Exception($saveLog['msg']); } $t->commit(); return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { \Yii::error($e); debug_log([__METHOD__, __LINE__, $e->getMessage()], __CLASS__ . '.log'); $t->rollBack(); return [ 'code' => 1, 'msg' => '操作失败,' . $e->getMessage() ]; } } public function getKefuInfo() { $mch_id = $this->mch_id; $mch = Mch::findOne($mch_id); if (!$mch) { return [ 'code' => 1, 'msg' => '数据不存在', ]; } $kefu_info = json_decode($mch->kefu_info ?: '', true); if (empty($kefu_info)) { $kefu_info = [ 'kefu_id' => '', 'kefu_auto_reply_img' => '', 'kefu_auto_reply_text' => '', ]; } $is_show_kefu_download = Option::get('is_show_kefu_download', 0, 'saas', 1)['value']; return [ 'code' => 0, 'msg' => 'success', 'data'=> [ 'kefu_id' => $kefu_info['kefu_id'] ?: '', 'kefu_auto_reply_img' => $kefu_info['kefu_auto_reply_img'] ?: '', 'kefu_auto_reply_text' => $kefu_info['kefu_auto_reply_text'] ?: '', 'is_show_kefu_download' => intval($is_show_kefu_download) ] ]; } public function setKefuInfo() { $kefu_id = $this->kefu_id; $kefu_auto_reply_img = $this->kefu_auto_reply_img; $kefu_auto_reply_text = $this->kefu_auto_reply_text; $mch_id = $this->mch_id; $mch = Mch::findOne($mch_id); if (!$mch) { return [ 'code' => 1, 'msg' => '数据不存在', ]; } $kefu_info = [ 'kefu_id' => $kefu_id ?: '', 'kefu_auto_reply_img' => $kefu_auto_reply_img ?: '', 'kefu_auto_reply_text' => $kefu_auto_reply_text ?: '', ]; if (!empty($kefu_id)) { if (!preg_match('/^1[3456789]\d{9}$/i', $kefu_id)) { return [ 'code' => 1, 'msg' => '客服手机号格式错误', ]; } $url = Kefu::API_HOST; $host = \str_replace(['http://', 'https://'], '', \Yii::$app->request->hostInfo); $res = http_post($url . '/api/regKefu', [ 'form_params' => [ 'token' => \Yii::$app->getKefuApiToken(), 'host' => $host, 'mobile' => $kefu_id, 'kefu_auto_reply_img' => $kefu_auto_reply_img, 'kefu_auto_reply_text' => $kefu_auto_reply_text, ] ]); if ($res->getStatusCode() != 200) { return [ 'code' => 1, 'msg' => '请求出错!', ]; } $content = json_decode((string)$res->getBody(), true); if ($content['code'] == 0) { $mch->kefu_info = json_encode($kefu_info, JSON_UNESCAPED_UNICODE); $mch->save(); } } else { $mch->kefu_info = json_encode($kefu_info, JSON_UNESCAPED_UNICODE); $mch->save(); $content = [ 'code' => 0, 'msg' => '保存成功', ]; } return $content; } //入驻商访客key public static function cacheKeyVisit($mch_id, $date = '') { $key = implode('_', ['mch_visit_date', $mch_id, $date]); return $key; } //入驻商访客 public function mchVisit($date = '', $add = 0) { $key = self::cacheKeyVisit($this->mch_id, $date); $val = cache()->get($key); if($add){ $val += $add; cache()->set($key, $val, 86400 * 30); } return $val; } }