| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models;
- use app\models\Option;
- use app\models\SaasUser;
- use app\models\Store;
- use app\models\User;
- use app\models\WechatConfig;
- use yii\base\Model;
- use app\models\SaasDistribution;
- use app\models\BusinessRightDuration;
- use app\models\BusinessRightInfo;
- use app\utils\Wechat\WechatBusinessPay;
- class SaasDistributionFrom extends Model
- {
- public $store_name;
- public function rules()
- {
- return [
- [['store_name'], 'string']
- ];
- }
- public function getDistribution(){
- $saas_id = get_saas_user_id();
- $saas_user = get_saas_user();
- $distributionInfo = SaasDistribution::find()->where(['saas_id'=>$saas_id,'is_delete'=>0])->asArray()->one();
- $rightDurationInfo = BusinessRightDuration::find()->where(['saas_id'=>$saas_id,'is_delete'=>0])->asArray()->all();
- $rightInfo = $rightIds = [];
- $is_expire = false;
- $start_time = time();
- $end_time = time() + (86400 * 3);
- foreach($rightDurationInfo as $val){
- if($val['expire_time'] != 0 && $val['expire_time'] >= $start_time && $val['expire_time'] <= $end_time ){
- $is_expire = true;
- }
- if($val['expire_time'] == 0 || $val['expire_time'] >= $start_time ){
- $rightIds[] = $val['right_id'];
- }
- }
- if(!empty($rightIds)){
- $rightInfo = BusinessRightInfo::resetRightInfo($rightIds, 'id,name,explanation');
- }else{
- $rightInfo = [];
- }
- if(empty($distributionInfo)){
- $distributionInfo['platform_open_id'] = '';
- $distributionInfo['ali_name'] = '';
- $distributionInfo['name'] = '';
- }
- $distributionInfo['is_expire'] = $is_expire;
- $distributionInfo['right_info'] = $rightInfo;
- $total_wechat_bind_count = 0;
- $current_wechat_bind_count = 0;
- if (\Yii::$app->prod_is_dandianpu()) {
- $init_store_ids = Option::find()->where([
- 'name' => 'self_mini',
- 'value' => 1
- ])->select('store_id')->column();//供应链独立店铺ID
- $init_store_ids = Store::find()->where(['id' => $init_store_ids, 'is_delete' => 0])->select('id')->column();
- $total_wechat_bind_count += count($init_store_ids);
- $current_wechat_bind_count = User::find()->where(['store_id' => $init_store_ids, 'is_delete' => 0])
- ->andWhere(['<>', 'wechat_open_id', ''])->select('wechat_open_id')->count();
- //非独立店铺ID
- $storeList = Store::find()->where([
- 'is_delete' => 0,
- 'business_model' => 1,
- ])->andWhere(['NOT IN', 'id', $init_store_ids])->select('id')->column();
- if ($storeList) {
- $total_wechat_bind_count += count($storeList);
- $user = User::find()->where(['is_delete' => 0, 'binding' => $saas_user->mobile, 'store_id' => $storeList])
- ->andWhere(['<>', 'wechat_open_id', ''])
- ->select('wechat_open_id')->asArray()->one();
- if ($user && $user['wechat_open_id']) {
- $current_wechat_bind_count += count($storeList);
- }
- }
- }
- if (\Yii::$app->prod_is_shangmengduli() || \Yii::$app->prod_is_shangmengduli() || \Yii::$app->prod_is_saas()) {
- $storeList = Store::find()->where([
- 'is_delete' => 0,
- 'business_model' => 1
- ])->select('id')->column();
- $total_wechat_bind_count = count($storeList);
- $current_wechat_bind_count = User::find()->where(['store_id' => $storeList, 'is_delete' => 0, 'binding' => $saas_user->mobile])
- ->andWhere(['<>', 'wechat_open_id', ''])->select('wechat_open_id')->count();
- $storeList = Store::find()->where([
- 'is_delete' => 0,
- 'business_model' => [2, 3, 4]
- ])->select('id')->column();
- if ($storeList) {
- $total_wechat_bind_count += count($storeList);
- if (!empty($saas_user->platform_open_id)) {
- $current_wechat_bind_count += count($storeList);
- }
- }
- }
- $distributionInfo['total_wechat_bind_count'] = $total_wechat_bind_count;
- $distributionInfo['current_wechat_bind_count'] = $current_wechat_bind_count;
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $distributionInfo
- ];
- }
- public function getDistributionStore() {
- try {
- $saas_user = get_saas_user();
- $store_name = $this->store_name;
- $is_dandianpu = 0;
- $is_dandianpu_bind = 0;
- //判断是否是供应链系统 且 是否是供应链小程序用户绑定过微信openid
- if (\Yii::$app->prod_is_dandianpu()) {
- $init_store_ids = Option::find()->where([
- 'name' => 'self_mini',
- 'value' => 1
- ])->select('store_id')->column();//供应链独立店铺ID
- $init_store_ids = Store::find()->where(['id' => $init_store_ids, 'is_delete' => 0])->select('id')->column();
- //非独立店铺ID
- $storeList = Store::find()->where([
- 'is_delete' => 0,
- 'business_model' => 1,
- ])->andWhere(['NOT IN', 'id', $init_store_ids])->select('id')->column();
- if ($storeList) {
- $user = User::find()->where(['is_delete' => 0, 'binding' => $saas_user->mobile, 'store_id' => $storeList])
- ->andWhere(['<>', 'wechat_open_id', ''])->select('wechat_open_id, created_at')->asArray()->one();
- if ($user) {
- $is_dandianpu_bind = 1;
- }
- }
- $is_dandianpu = 1;
- }
- $query = Store::find()->where(['is_delete' => 0])->select('id, name, logo, business_model')->orderBy('id desc');
- if (trim($store_name) && !empty($store_name)) {
- $query->andWhere(['like', 'name', $store_name]);
- }
- $pagination = pagination_make($query,true);
- foreach($pagination['list'] as &$value){
- $is_bind = 0;
- if ($is_dandianpu) {
- $self_mini = Option::get('self_mini', $value['id'], 'store', 0)['value'];
- if (intval($self_mini)) {
- $value['appid'] = WechatConfig::findOne(['store_id' => $value['id'], 'type' => 1])->app_id ?: '';
- $user = User::find()->where(['store_id' => $value['id'], 'is_delete' => 0, 'binding' => $saas_user->mobile])
- ->andWhere(['<>', 'wechat_open_id', ''])->select('wechat_open_id, created_at')->asArray()->one();
- if ($user) {
- $is_bind = 1;
- }
- $value['path'] = "pages/home/home";
- } else {
- $value['appid'] = Option::get('one_store_wechat_appid', 0, 'saas')['value'] ?: '';
- $value['path'] = "pages/home/home?store_id={$value['id']}";
- $is_bind = $is_dandianpu_bind;
- }
- } else {
- if (intval($value['business_model']) === 1) {
- $user = User::find()->where(['store_id' => $value['id'], 'is_delete' => 0, 'binding' => $saas_user->mobile])
- ->andWhere(['<>', 'wechat_open_id', ''])->select('wechat_open_id, created_at')->asArray()->one();
- if ($user) {
- $is_bind = 1;
- }
- $wechatConfig = WechatConfig::findOne(['store_id' => $value['id'], 'type' => 1]);
- $value['appid'] = $wechatConfig->app_id ?: '';
- $value['path'] = "pages/home/home";
- } else {
- $is_bind = intval($saas_user->platform_open_id);
- $value['appid'] = Option::get('platform_appid', 0, 'saas')['value'] ?: '';
- if (intval($value['business_model']) === 2) {
- $value['path'] = "pages/shop/shopIndex?store_id={$value['id']}";
- } elseif (intval($value['business_model']) === 3) {
- $value['path'] = "pages/shop/shopDetail?store_id={$value['id']}";
- } elseif (intval($value['business_model']) === 4) {
- $value['path'] = "alipay-order/orderMeal/orderMeal?store_id={$value['id']}";
- }
- if ($is_bind) {
- $value['created_at'] = date('Y-m-d H:i:s', $saas_user->created_at);
- }
- }
- }
- $value['created_at'] = isset($user) ? date('Y-m-d H:i:s', $user['created_at']) : ($value['created_at'] ?: '');
- $value['is_bind'] = $is_bind;
- }
- $pagination['user_info'] = [
- 'name' => $saas_user->name,
- 'avatar' => $saas_user->avatar,
- 'level_name' => '店铺会员'
- ];
- return [
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => $pagination
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- public function wechatDistribution($code,$open_id){
- if(empty($open_id) && empty($code)){
- return [
- 'code' => 1,
- 'msg' => '参数不能为空',
- ];
- }
- $type = 0;
- if(empty($open_id)){
- $wechat_mini = WechatBusinessPay::BussinessWechatMini();
- $mini_res = $wechat_mini->auth->session($code);
- if(!isset($mini_res['openid'])){
- return [
- 'code' => 1,
- 'msg' => '获取微信息失败',
- 'res' => $mini_res,
- ];
- }
- $open_id = $mini_res['openid'];
- $type = 1; //小程序获取
- }else{
- $type = 2; //app获取
- }
- $saas_id = get_saas_user_id();
- $distributionInfo = SaasDistribution::findOne(['saas_id'=>$saas_id]);
- if(!$distributionInfo){
- $distributionInfo = new SaasDistribution();
- $distributionInfo->created_at = time();
- }
- $distributionInfo->wechat_type = $type;
- $distributionInfo->saas_id = $saas_id;
- $distributionInfo->platform_open_id = $open_id;
- $distributionInfo->bank = '';
- if($distributionInfo->save() ){
- return [
- 'code' => 0,
- 'msg' => '设置成功'
- ];
- }else{
- return [
- 'code' => 1,
- 'msg' => '设置失败'
- ];
- }
- }
- public function aliDistribution($name,$ali_name){
- $saas_id = get_saas_user_id();
- $distributionInfo = SaasDistribution::findOne(['saas_id'=>$saas_id]);
- if(!$distributionInfo){
- $distributionInfo = new SaasDistribution();
- $distributionInfo->created_at = time();
- }
- $distributionInfo->saas_id = $saas_id;
- $distributionInfo->name = $name;
- $distributionInfo->ali_name = $ali_name;
- $distributionInfo->bank = '';
- if($distributionInfo->save() ){
- return [
- 'code' => 0,
- 'msg' => '设置成功'
- ];
- }else{
- return [
- 'code' => 1,
- 'msg' => $distributionInfo->errors
- ];
- }
- }
- public function bankDistribution($saas_id, $conf){
- if(is_string($conf)){
- $conf = json_decode($conf, true);
- }
- if(!$conf || empty($conf)){
- return [
- 'code' => 1,
- 'msg' => '参数错误',
- ];
- }
- $distributionInfo = SaasDistribution::findOne(['saas_id'=>$saas_id]);
- if(!$distributionInfo){
- $distributionInfo = new SaasDistribution();
- $distributionInfo->created_at = time();
- }
- $distributionInfo->saas_id = $saas_id;
- $distributionInfo->bank = json_encode($conf);
- if($distributionInfo->save() ){
- return [
- 'code' => 0,
- 'msg' => '设置成功'
- ];
- }else{
- return [
- 'code' => 1,
- 'msg' => $distributionInfo->errors
- ];
- }
- }
- }
|